Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ArgumentException: Invalid JSON primitive error

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?

like image 670
Ortal Avatar asked Jan 03 '12 14:01

Ortal


2 Answers

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.

like image 53
Jack Liu Shurui Avatar answered Sep 28 '22 01:09

Jack Liu Shurui


            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},
like image 23
eaglei22 Avatar answered Sep 28 '22 00:09

eaglei22