问题
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.
回答1:
The first layered object names in the JSON should be the same names as your webservice arguments names.
{"data": {"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"} }
回答2:
Try this,
var params = {"PostData":{"Guid":"b61dce32-690b-4409-b51a-a6012462e54e","Action":"Testing","Id":"3"}};
var yourdata = jQuery.param(params);
pass
yourdata
as your data instead of JSON.stringify(b).
来源:https://stackoverflow.com/questions/4954628/pagemethods-jquery-and-json