消息:无效的JSON原语:带有Webmethod的ajax jquery方法 [英] Message: Invalid JSON primitive: ajax jquery method with Webmethod
问题描述
I am using Data value as object literal, instead of concatenating a String as explained in this answer
我的代码如下:
$.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)
{
...
}
但是出现以下错误:
消息:无效的JSON原语:projectSoid
Message: Invalid JSON primitive: projectSoid
推荐答案
使用您的contentType: 'application/json; charset=utf-8'
声称您将发送JSON,但当前您的data
属性不包含JSON.
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.
您需要使用JSON.stringify
方法将data
转换为JSON:
You need to transform your data
to JSON with the JSON.stringify
method:
因此将您的data
属性更改为:
So change your data
property to:
data: JSON.stringify({
"projectSoid": ProjectId,
"startDate": StartDate,
"endDate": EndDate,
"clientManager": ClientManager
}),
您应注意,较旧的浏览器本身不支持JSON.stringify
方法,因此您可能需要使用以下各种库之一来提供实现:
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的 JSON2库.
Douglas Crockford's JSON2 library.
这篇关于消息:无效的JSON原语:带有Webmethod的ajax jquery方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!