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.

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188

2 Answers2

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"} }
jerone
  • 16,206
  • 4
  • 39
  • 57
  • yep, I knew I'm doing something silly but was not able to nail it. Thanks @jerone. – TheVillageIdiot Feb 10 '11 at 08:22
  • Thank You! I can't tell you how long I hunted around to see why I was getting a "500" error on my jquery.post. I never even thought of having to name my WebMethod argument inside the JSON I was sending from the client. – dagilleland Jun 30 '12 at 01:05
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).

Furqan Hameedi
  • 4,372
  • 3
  • 27
  • 34