我有這樣的代碼: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庫。
