首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Ajax处理中的“无效JSON原语”

Ajax处理中的“无效JSON原语”
EN

Stack Overflow用户
提问于 2010-03-15 16:43:02
回答 11查看 274.2K关注 0票数 109

我在来自jQuery的ajax调用中收到一个错误。

下面是我的jQuery函数:

function DeleteItem(RecordId, UId, XmlName, ItemType, UserProfileId) {
    var obj = {
        RecordId: RecordId,
        UserId: UId,
        UserProfileId: UserProfileId,
        ItemType: ItemType,
        FileName: XmlName
    };
    var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);

    $.ajax({
        type: "POST",
        url: "EditUserProfile.aspx/DeleteRecord",
        data: json,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function(msg) {
            if (msg.d != null) {
                RefreshData(ItemType, msg.d);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("error occured during deleting");
        }
    });
}

这是我的WebMethod

[WebMethod]
public static string DeleteRecord(Int64 RecordId, Int64 UserId, Int64 UserProfileId, string ItemType, string FileName) {
    try {
        string FilePath = HttpContext.Current.Server.MapPath(FileName);

        XDocument xmldoc = XDocument.Load(FilePath);
        XElement Xelm = xmldoc.Element("UserProfile");
        XElement parentElement = Xelm.XPathSelectElement(ItemType + "/Fields");

        (from BO in parentElement.Descendants("Record")
         where BO.Element("Id").Attribute("value").Value == RecordId.ToString()
         select BO).Remove();
        XDocument xdoc = XDocument.Parse(Xelm.ToString(), LoadOptions.PreserveWhitespace);
        xdoc.Save(FilePath);

        UserInfoHandler obj = new UserInfoHandler();
        return obj.GetHTML(UserId, UserProfileId, FileName, ItemType, RecordId, Xelm).ToString();
    } catch (Exception ex) {
        HandleException.LogError(ex, "EditUserProfile.aspx", "DeleteRecord");
    }
    return "success";
}

有人能告诉我我的代码出了什么问题吗?

我得到了这个错误:

{
    "Message":"Invalid JSON primitive: RecordId.",
    "StackTrace":"
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
       at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
       at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
       at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)
       at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)
       at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
    "ExceptionType":"System.ArgumentException"
}
EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-03-15 17:30:18

只需猜测变量json后面包含的内容

var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);?

如果它是一个有效的json对象,比如{"foo":"foovalue", "bar":"barvalue"},那么jQuery可能不会将其作为json数据发送,而是将其序列化为foor=foovalue&bar=barvalue,因此您会得到错误"Invalid JSON primitive: foo"

请尝试将数据设置为字符串

$.ajax({
    ...
    data: '{"foo":"foovalue", "bar":"barvalue"}', //note the additional quotation marks
    ...
})

这样,jQuery应该保留数据,并按原样将字符串发送到服务器,服务器应该允许ASP.NET解析json服务器端。

票数 143
EN

Stack Overflow用户

发布于 2011-11-26 07:38:05

使用

data : JSON.stringify(obj)

在上面的情况下,我相信应该是可行的。

注意:您应该添加json2.js库所有浏览器都不支持JSON对象(IE7-) Difference between json.js and json2.js

票数 115
EN

Stack Overflow用户

发布于 2016-09-07 23:40:19

它的工作原理是这样的

data: JSON.stringify({'id':x}),
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2445874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档