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 + "
" + $.evalJSON(msg.d)); }, error: function (x, y) { var t = $(c).html(); $(c).html(t + "
" + $.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) 

但没有在哪里。

JSON中的第一个分层对象名称应与webservice参数名称相同。

 {"data": {"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"} } 

试试这个,

  var params = {"PostData":{"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}}; var yourdata = jQuery.param(params); 

通过

yourdata

作为您的数据而不是JSON.stringify(b)。