1

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.

4

2 回答 2

2

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"} }
于 2011-02-10T08:05:37.330 回答
0

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).

于 2011-02-10T08:01:43.963 回答