AJAX Data 传值 无效的JSON基元:AJAX jQuery的方法,用c#WEBMETHOD-c#,jquery.

我有这样的代码:I数据值,而不是连接字符串作为对象常量。为什么?看到这里 我的代码是这样的:-

$.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]
  public static string SaveClient(string projectSoid, string startDate, string endDate, string clientManager)
  {}

问题是我得到的错误是这样的: 消息:无效的JSON基元:projectSoid

1. 有了您的contentType: 'application/json; charset=utf-8'你是否认为你会送JSON,但目前你data属性不持有的JSON。 您需要将您的data到JSON与JSON.stringify方法: 因此,改变你data属性为:

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

你应该注意的是,JSON.stringify方法本身不支持在旧的浏览器,因此您可能需要提供像图书馆的一项: douglasCrockford的JSON2库。

原文地址:https://www.cnblogs.com/520cc/p/4258978.html