views:

1372

answers:

4

Hi

I have the following jquery code to call a webmethod in an aspx page

$.ajax({
    type: "POST",
    url: "popup.aspx/GetJewellerAssets",
    contentType: "application/json; charset=utf-8",
    data: '{"jewellerId":' + filter + '}',
    dataType: "json",
    success: AjaxSucceeded,
    error: AjaxFailed
});

and here is the web method signature

[WebMethod]
public static string GetJewellerAssets(int jewellerId)
{

This works fine.

But now I need to get two parameters passed to the web method

the new web method looks like this

[WebMethod]
public static string GetJewellerAssets(int jewellerId, string locale)
{
}

How do I change the client code to successfully call this new method signature ?

EDIT:

The following 2 syntaxes worked

data: '{ "jewellerId":' + filter + ', "locale":"en" }',

and

data: JSON.stringify({ jewellerId: filter, locale: locale }),

where filter and locale are local variables

+2  A: 
data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}',
David Hedlund
this worked: '{ "jewellerId":' + filter + ', "locale":"en" }', (obviously I won't be hardcoding the locale to en, but this is the syntax that worked.
Christo Fur
+1  A: 

simply add as many properties as you need to the data object.

 $.ajax({
                    type: "POST",
                    url: "popup.aspx/GetJewellerAssets",
                    contentType: "application/json; charset=utf-8",
                    data: {jewellerId: filter , foo: "bar", other: "otherValue"},
                    dataType: "json",
                    success: AjaxSucceeded,
                    error: AjaxFailed
                });
pixeline
+5  A: 

Don't use string concatenation to pass parameters, just use a data hash:

$.ajax({
    type: 'POST',
    url: 'popup.aspx/GetJewellerAssets',
    contentType: 'application/json; charset=utf-8',
    data: { jewellerId: filter, locale: 'en-US' },
    dataType: 'json',
    success: AjaxSucceeded,
    error: AjaxFailed
});


UPDATE:

As suggested by @Alex in the comments section, an ASP.NET PageMethod expects parameters to be JSON encoded in the request, so JSON.stringify should be applied on the data hash:

$.ajax({
    type: 'POST',
    url: 'popup.aspx/GetJewellerAssets',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ jewellerId: filter, locale: 'en-US' }),
    dataType: 'json',
    success: AjaxSucceeded,
    error: AjaxFailed
});
Darin Dimitrov
Consider also using `JSON.stringify( myObject )` to create a JSON string from a javascript object, in case you want to group your parameters to a class later on.
Alex Bagnolini
thanks for the replies - however, I get a http status 500 when I try it like that. Any ideas? or even how to debug it? Breakpoint in the web method never gets hit
Christo Fur
new code as follows$.ajax({ type: 'POST', url: 'popup.aspx/GetJewellerAssets', contentType: 'application/json; charset=utf-8', data: {jewellerId:filter,locale:'en-US'}, dataType: "json", success: AjaxSucceeded, error: AjaxFailed });
Christo Fur
To debug, first look at FireBug what's the exact response of the server, then put a break point in the web service method and see if it is reached.
Darin Dimitrov
Breakpoint in the web method never gets hitFirebug shows: "Message":"Invalid JSON primitive: jewellerId.","StackTrace":" at System.Web.Script.SerializationSo I guess the syntax of the json is incorrect
Christo Fur
As @Alex suggested you need to use `JSON.stringify` like this: `data: JSON.stringify({ jewellerId: 1, locale: 'en-US' })`
Darin Dimitrov
thanks a lot for this, it worked a treat
Christo Fur
+1  A: 

Has anyone else noticed that the json string/object is invalid in all answers except for David Hedlund's? :)

JSON objects must be formatted in the following manner: {"key": ("value" | 0 | false)}. Also, writing it out as a string requires much less than stringifying the object...

Ariel