jQuery System.ArgumentException:无效的 JSON 原始错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8713843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:56:50  来源:igfitidea点击:

System.ArgumentException: Invalid JSON primitive error

jqueryasp.net-mvcjson

提问by Ortal

I am getting "System.ArgumentException: Invalid JSON primitive: pagenum" when I return "sdata" in the following code:

当我在以下代码中返回“sdata”时,我收到“System.ArgumentException:无效的 JSON 原语:pagenum”:

 function getPageData() {
pagenum = parseInt(eSc("#resultsBtn").attr("data-pagenum"));
if (pageName === "Home") {
    scrollPath = "/Home/GetResults/";
    sdata = { "pagenum": pagenum, "sortType": sortType };
}
else if (pageName === "Search") {
    scrollPath = "/SearchAjax/GetResultsKeyword/";
    sdata = { "pagenum": pagenum, "sortType": sortType, "keyword": keyword };
}
else if (pageName === "Cat") {
    scrollPath = "/SearchAjax/GetResultsCategory/";
    sdata = { "pagenum": pagenum, "sortType": sortType, "ID": categoryId, "Level": level };
}
else if (pageName === "Merchant") {
    scrollPath = "/SearchAjax/GetResultsMerchant/";
    sdata = { "pagenum": pagenum, "sortType": sortType, "ID": merchantId };
}

}

}

and the init function on pageload:

以及页面加载上的 init 函数:

 function init(a, b, c, d, e, f, g) {
getPageData();
eSc.ajax({
    type: 'POST',
    url: scrollPath,
    data: sdata,
    success: function (data) {
        eSc("#moreResults").html(data);
    }
});

}

}

users dont see an issue and the correct data is still returned, yet I am getting an error email every time someone loads more data from our site in production (doesnt happen in development so its hard to troubleshoot). When inspecting in firebug, I see the correct data is passed. So why am I still getting this error?!

用户没有看到问题,仍然返回正确的数据,但每次有人从我们的生产站点加载更多数据时,我都会收到一封错误电子邮件(在开发中不会发生,因此很难排除故障)。在萤火虫中检查时,我看到传递了正确的数据。那么为什么我仍然收到此错误?!

Any tips as to why this might be happening?

关于为什么会发生这种情况的任何提示?

回答by Hyman Liu Shurui

function init(a, b, c, d, e, f, g) {
getPageData();
eSc.ajax({
    type: 'POST',
    url: scrollPath,
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(sdata ),
    success: function (data) {
        eSc("#moreResults").html(data);
    }
});

pass sData in json format, using JSON.stringify to format data in json format.

以json格式传递sData,使用JSON.stringify将数据格式化为json格式。

It works in my case. Hope it work in your case.

它适用于我的情况。希望它适用于您的情况。

回答by eaglei22

            var param = "{'type': '" + type + "'}";
            var paramSfy = JSON.stringify({ type: type})
            var src = '/Physical_Inventory/Home/runZeroQtyDLIUpdate';
            $.ajax({
                type: "POST",
                url: src,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: paramSfy,

What I noticed is,

我注意到的是,

if you are using contentType: "application/json; charset=utf-8" then the data is expected as a string:

如果您使用的是 contentType: "application/json; charset=utf-8" 那么数据应该是一个字符串:

"{ "Param" : "Value" }"

this is best accomplished by using the JSON.stringify function.

这最好通过使用 JSON.stringify 函数来完成。

if you don't set a content type then the default is, "application/x-www-form-urlencoded; charset=UTF-8" If you use this content type, then the param and value are embeded into the url, and the data can be set in ajax like so:

如果您不设置内容类型,则默认值为 "application/x-www-form-urlencoded; charset=UTF-8" 如果您使用此内容类型,则将参数和值嵌入到 url 中,并且数据可以像这样在ajax中设置:

data: {Param : Value},

回答by tildy

jQuery serializes $.ajax()'s data parameter using the URL encoded scheme, regardless of what Content-Type is specified. I recommend to use the content type in ajax:

jQuery 使用 URL 编码方案序列化 $.ajax() 的数据参数,无论指定什么 Content-Type。我建议在 ajax 中使用内容类型:

function init(a, b, c, d, e, f, g) {
getPageData();
eSc.ajax({
    type: 'POST',
    url: scrollPath,
    contentType: 'application/json',
    dataType: 'json',
    data: sdata,
    success: function (data) {
        eSc("#moreResults").html(data);
    }
});

Also you need to use quotes in data parameter. In your version it's a JavaScript object literal instead of JSON string.

您还需要在数据参数中使用引号。在您的版本中,它是一个 JavaScript 对象文字而不是 JSON 字符串。

function getPageData() {
pagenum = parseInt(eSc("#resultsBtn").attr("data-pagenum"));
if (pageName === "Home") {
    scrollPath = "/Home/GetResults/";
    sdata = '{ "pagenum":'+ pagenum +' , "sortType":'+ sortType +' }';
}
else if (pageName === "Search") {
    scrollPath = "/SearchAjax/GetResultsKeyword/";
    sdata = '{ "pagenum": ' + pagenum + ', "sortType": '+ sortType +', "keyword": ' + keyword +' }';
}
else if (pageName === "Cat") {
    scrollPath = "/SearchAjax/GetResultsCategory/";
    sdata = '{ "pagenum":'+ pagenum + ', "sortType":'+ sortType +', "ID":'+ categoryId +', "Level": '+level+' }';
}
else if (pageName === "Merchant") {
    scrollPath = "/SearchAjax/GetResultsMerchant/";
    sdata = '{ "pagenum":'+ pagenum +', "sortType":'+ sortType + ', "ID":'+ merchantId +'}';
}

I hope it helps.

我希望它有帮助。