Message: Invalid JSON primitive: ajax jquery metho

2019-02-05 20:20发布

问题:

I am using Data value as object literal, instead of concatenating a String as explained in this answer

My code is the following:

$.ajax({    
  url: "../Member/Home.aspx/SaveClient",
  type: "POST",
  async: false,
  dataType: 'json',
  contentType: 'application/json; charset=utf-8',
  data: {
    "projectSoid": ProjectId,
    "startDate": StartDate,
    "endDate": EndDate,
    "clientManager": ClientManager
  },
  success: function(response) {
    if (response.d != "") {

    }
  },
  error: function(response) {
    var r = jQuery.parseJSON(response.responseText);
    alert("Message: " + r.Message);
    alert("StackTrace: " + r.StackTrace);
    alert("ExceptionType: " + r.ExceptionType);
  }
})

and my webmethod is like this :

[WebMethod]
public static string SaveClient(string projectSoid, string startDate, 
     string endDate, string clientManager)
{
    ...
}

But I get the following error:

Message: Invalid JSON primitive: projectSoid

回答1:

With your contentType: 'application/json; charset=utf-8' you are claiming that you will send JSON but currently your data property is not holding JSON.

You need to transform your data to JSON with the JSON.stringify method:

So change your data property to:

data: JSON.stringify({
    "projectSoid": ProjectId,
    "startDate": StartDate,
    "endDate": EndDate,
    "clientManager": ClientManager
}),

You should note that the JSON.stringify method is not natively supported in older browsers so you may need to provide an implementation with using one of the various libraries like:

Douglas Crockford's JSON2 library.



回答2:

Javascript at Client side

 var items = [{ projectSoid: ProjectId, startDate: StartDate, endDate: EndDate, clientManager: ClientManager }];


                   $.ajax({
                       url: '"../Member/Home.aspx/SaveClient',
                       type: "POST",
                       data: JSON.stringify({ items: items }),

                       //data:  JSON.stringify("{DocKey : '" + DocKey + "',highlightText: '" +  JSON.stringify(text) + "',pageNo: '" + pgNo + "',left: '" + left + "',top: '" + top + "',width: '" + width + "',height: '" + height + "'}"),

                       //data: "{DocKey\":\""+ DocKey+"\",\"highlightText\":\""+ text +"\",\"pageNo\":\""+pgNo+"\",\"left\":\""+left+"\",\"top\":\""+top+",\"width\":\""+width+"\",\"height\":\""+ height +"}}",
                       // data: "{DocKey : '" + DocKey + "',highlightText: '" + text + "',pageNo: '" + pgNo + "',left: '" + left + "',top: '" + top + "',width: '" + width + "',height: '" + height + "'}",
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       beforeSend: function () {
                           alert("Start!!! ");
                       },
                       success: function (data) {
                           alert("Save data Successfully");
                       },
                       failure: function (msg) { alert("Sorry!!! "); evt.obj.deleteObject(); },
                       async: false

                   });

Web Method at Code behind

[WebMethod]       
 public static string SaveClient(object items)       {

    List<object> lstItems = new     JavaScriptSerializer().ConvertToType<List<object>>(items);

  Dictionary<string, object> dic = (Dictionary<string, object>)lstItems[0];

    }