5

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?

1

3 Answers 3

14
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.

Sign up to request clarification or add additional context in comments.

Comments

4
            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

0

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.

3 Comments

great, however i am getting the following error (even though the response is correct in firebug)
sorry, hit enter too soon: "great, however i am getting the following error (even though the response is correct in firebug) "SyntaxError: JSON.parse: unexpected character" i tried commenting out the contentTupe (I am not getting json back) and have the same issue."
I edited the code, because + sign were missing. Please , try it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.