Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid JSON primitive ERROR

Tags:

jquery

ajax

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

like image 753
Nithin Paul Avatar asked Jul 26 '13 05:07

Nithin Paul


People also ask

What is JSON primitive?

JSON can represent (sub)values of four primitive data types and of two compound data types. The primitive data types are string, number, boolean, and null. There is no way to declare the data type of a JSON value; rather, it emerges from the syntax of the representation.


2 Answers

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}), 
like image 174
UltraInstinct Avatar answered Sep 22 '22 21:09

UltraInstinct


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

like image 20
Akki619 Avatar answered Sep 24 '22 21:09

Akki619