Invalid JSON primitive ERROR

后端 未结 3 1667
一向
一向 2020-12-29 19:50

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

    $.ajax({
                url: \"/Precedent/         


        
相关标签:
3条回答
  • 2020-12-29 20:36

    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}),
    
    0 讨论(0)
  • 2020-12-29 20:36

    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

    0 讨论(0)
  • 2020-12-29 20:37

    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.

    0 讨论(0)
提交回复
热议问题