我试图像这样使用jQuery调用PageMethod
:
[WebMethod]
public stataic string WebMethod(PostData data)
{
//DO WORK
return "a";
}
PostData
课程如下:
public class PostData
{
public string Guid{get;set;}
public string Action{get;set;}
public string Id{get;set;}
}
我正在调用jQuery中的方法:
$.ajax({
type="POST",
url: 'url',
data: JSON.stringify(b),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (msg) {
var t = $(c).html();
$(c).html(t + "<br/>" + $.evalJSON(msg.d));
},
error: function (x, y) {
var t = $(c).html();
$(c).html(t + "<br/>" + $.evalJSON(x.responseText).Message);
}
});
b
就像:{"PostData":{"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}}
我收到了这个错误:
Invalid web service call, missing value for parameter: 'data'.
如果我不致电JSON.stringyfy
,我会收到此错误:
Invalid JSON primitive: PostData.
我也试过这个{"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}
,但仍然得到
Invalid JSON primitive 'Guid'
或
Invalid web service call, missing value for parameter: 'data'.
取决于我是否拨打JSON.stringify
。
我也试过了,
[WebMethod]
public static string WebMethod(string data)
但没有在哪里。
答案 0 :(得分:2)
JSON中的第一个分层对象名称应与您的Web服务参数名称相同。
{"data": {"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"} }
答案 1 :(得分:0)
试试这个,
var params = {"PostData":{"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}};
var yourdata = jQuery.param(params);
传递
yourdata
代替JSON.stringify(b)。