PageMethods、jQuery 和 JSON
我正在尝试使用 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);
}
});
where 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'
or
Invalid web service call, missing value for parameter: 'data'.
取决于我是否调用 JSON.stringify
。
我也尝试过,
[WebMethod]
public static string WebMethod(string data)
但是没有地方。
I am trying to call a PageMethod
using jQuery like this:
[WebMethod]
public stataic string WebMethod(PostData data)
{
//DO WORK
return "a";
}
PostData
class is as follows:
public class PostData
{
public string Guid{get;set;}
public string Action{get;set;}
public string Id{get;set;}
}
I'm calling method from jQuery like this:
$.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);
}
});
where b
is like: {"PostData":{"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}}
I'm getting this error:
Invalid web service call, missing value for parameter: 'data'.
If I don't call JSON.stringyfy
then I get this error:
Invalid JSON primitive: PostData.
I have tried this also {"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}
but still get either
Invalid JSON primitive 'Guid'
or
Invalid web service call, missing value for parameter: 'data'.
depending on if I call JSON.stringify
or not.
I have also tried,
[WebMethod]
public static string WebMethod(string data)
but got no where.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JSON 中的第一个分层对象名称应与 Web 服务参数名称相同。
The first layered object names in the JSON should be the same names as your webservice arguments names.
试试这个,
通过
作为您的数据而不是 JSON.stringify(b)。
Try this,
pass
as your data instead of JSON.stringify(b).