消息:无效的JSON原始:阿贾克斯jQuery的方法,用C#的WebMethod [英] Message: Invalid JSON primitive: ajax jquery method with c# Webmethod
问题描述
我有code是这样的:我使用的数据值作为对象文本,而不是连接字符串。为什么? <一href=\"http://stackoverflow.com/questions/15293091/sending-multiple-data-parameters-with-single-quote-with-jquery-ajax/15293209?noredirect=1#comment21581381_15293209\">see这里
我的code是这样的: -
$。阿贾克斯({
网址:../Member/Home.aspx/SaveClient
键入:POST,
异步:假的,
数据类型:JSON,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:{
projectSoid:专案编号,
的startDate:起始日期,
结束日期:结束日期,
clientManager:ClientManager
},
成功:函数(响应){
如果(response.d!=){ }
},
错误:功能(响应){
变种R = jQuery.parseJSON(response.responseText);
警报(消息:+ r.Message);
警报(堆栈跟踪:+ r.StackTrace);
警报(ExceptionType:+ r.ExceptionType);
}
})
和的webmethod是这样的:
[的WebMethod]
公共静态字符串SaveClient(字符串projectSoid,字符串的startDate,字符串结束日期,串clientManager)
{}
问题是我得到的错误是这样的:
消息:无效的JSON原始:projectSoid
块引用>解决方案通过您的
的contentType:应用/ JSON的;字符集= UTF-8
你声称,你将发送JSON,但目前你的数据
属性未持有JSON。您需要将您的
数据
与JSON.stringify
变换方法,以JSON:因此,改变你的
数据
属性:数据:JSON.stringify({
projectSoid:专案编号,
的startDate:起始日期,
结束日期:结束日期,
clientManager:ClientManager
}),您应该注意的是,
JSON.stringify
方法本身并不在旧的浏览器的支持,所以你可能需要提供一个实现使用各种库像之一:道格拉斯Crockford的 JSON2库。
I have code like this : i am using Data value as object literal, instead of concatenating a String. WHY? see here
my code is this:-
$.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 webmethod is like this :
[WebMethod] public static string SaveClient(string projectSoid, string startDate, string endDate, string clientManager) {}
Problem is i got error like this:
Message: Invalid JSON primitive: projectSoid
解决方案With your
contentType: 'application/json; charset=utf-8'
you are claiming that you will send JSON but currently yourdata
property is not holding JSON.You need to transform your
data
to JSON with theJSON.stringify
method:So change your
data
property to: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:Douglas Crockford's JSON2 library.
这篇关于消息:无效的JSON原始:阿贾克斯jQuery的方法,用C#的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!