I am getting "System.ArgumentException: Invalid JSON primitive: pagenum" when I return "sdata" in the following code:
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:
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?
3 Answers 3
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.
It works in my case. Hope it work in your case.
Comments
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:
"{ "Param" : "Value" }"
this is best accomplished by using the JSON.stringify function.
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:
data: {Param : Value},
Comments
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:
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.
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.
JSON.stringifysolves the problem: encosia.com/asmx-scriptservice-mistake-invalid-json-primitive