32

Please Help. In my ajax call getting error Invalid JSON primitive, whats wrong with this following ajax call

    $.ajax({
                url: "/Precedent/ShowPartyContents", type: "POST",
                contentType: 'application/json; charset=utf-8',
                dataType: 'html',
                data:{'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId},
                sucess:function(result){
                    alert("String"+ result);
                    //jq("#PartyTagContentArea-"+ pass cheyyenda id).html(data).fadeIn();
                },
                error : function( ts ){ 
                    alert("error :(" + ts.responseText);


                }

            });

Thanks

Nithin Paul
  • 2,169
  • 3
  • 33
  • 55

3 Answers3

71

You are promising a content type of application/json but are sending a plain JS Object, which gets serialised as percentile-encoded-string by jQuery. This serialization might be far from valid JSON.

Change:

data: {'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId},

to:

data: JSON.stringify({'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId}),
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
  • This reminded me that for the JSon response it should be a contenttype of application/json but for initially submitting via JavaScript it needed to be 'application/x-www-form-urlencoded;charset=UTF-8' – Paul Zahra Mar 29 '16 at 09:37
  • Any idea how we could be getting this error, only from an iPhone client? There are multiple installations of something I'm working on, working fine for thousands of users, except some people using the latest iPhone client "Invalid JSON primitive: POST" – PandaWood Jun 23 '16 at 00:37
4

Try with, remove " ' " from data,

data:{partyId:party,PartySelCombo:valueFrom,DocumentId:DocId}

Use single quote to assign your values like

Wrong:

$.ajax({
  type: 'POST',
  contentType: 'application/json',
  dataType: 'json',
  url: 'WebService.asmx/Hello',
  data: { FirstName: "Dave", LastName: "Ward" }
});

Right:

$.ajax({
  type: 'POST',
  contentType: 'application/json',
  dataType: 'json',
  url: 'WebService.asmx/Hello',
  data: '{ FirstName: "Dave", LastName: "Ward" }'
});

Please follow below link for clarifications

Invalid Json Premitive Possible Reason

josliber
  • 43,891
  • 12
  • 98
  • 133
Akki619
  • 2,386
  • 6
  • 26
  • 56
1

You are facing the problem due to these lines:

contentType: 'application/json; charset=utf-8',
dataType: 'html',

first you are saying to application that the return result will be JSON type and in second line you say that the dataType will be HTML. Then how can it be return the json data.

To return and use the json data, you must specify the dataType:'json'. Use this:

contentType: 'application/json; charset=utf-8',
dataType: 'json',

Note: you have misspelled the success function so correct that also.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • 1
    `contentType` applies only to the data jQuery *sends*. `dataType` applies only to the data jQuery *receives*. It is correct that `contentType: 'application/json; charset=utf-8'` needs to be removed (because with the other settings at their defaults, jQuery will form-encode the data which will not be json), but `dataType` needs to stay `'html'` if that is the kind of response that is expected from the server. – GSerg Oct 17 '17 at 07:19