C# 消息:无效的 JSON 原语:ajax jquery 方法与 Webmethod
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15293717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Message: Invalid JSON primitive: ajax jquery method with Webmethod
提问by Nestor C
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
消息:无效的 JSON 原语:projectSoid
回答by nemesv
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.
您contentType: 'application/json; charset=utf-8'
声称您将发送 JSON,但目前您的data
财产不持有 JSON。
You need to transform your data
to JSON with the JSON.stringify
method:
您需要data
使用以下JSON.stringify
方法将您的转换为 JSON :
So change your data
property to:
因此,将您的data
属性更改为:
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:
您应该注意,JSON.stringify
旧浏览器本身不支持该方法,因此您可能需要使用各种库之一来提供实现,例如:
Douglas Crockford's JSON2 library.
Douglas Crockford 的JSON2 库。
回答by Hassan Nazeer
Javascript at Client side
客户端的 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 Method at Code behind
代码后面的 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];
}