消息:无效的JSON原语:ajax jquery方法与Webmethod

本文关键字:jquery 方法 Webmethod ajax 原语 无效 JSON 消息 | 更新日期: 2023-09-27 18:02:51

我使用数据值作为对象文字,而不是像这个答案中解释的那样连接字符串

我的代码如下:
$.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);
  }
})

和我的webmethod是这样的:

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

但是我得到以下错误:

消息:无效的JSON原语:projectSoid

消息:无效的JSON原语:ajax jquery方法与Webmethod

对于您的contentType: 'application/json; charset=utf-8',您声称您将发送JSON,但目前您的data属性不持有JSON。

您需要使用JSON.stringify方法将data转换为JSON:

data属性改为:

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

您应该注意,JSON.stringify方法在较旧的浏览器中不受本机支持,因此您可能需要使用各种库之一来提供实现,例如:

Douglas Crockford的JSON2库

客户端Javascript

 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方法在

[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];
    }