Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Multiple Parameters to jQuery ajax call

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

like image 686
ChrisCa Avatar asked Dec 16 '09 17:12

ChrisCa


1 Answers

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 }); 
like image 181
Darin Dimitrov Avatar answered Sep 17 '22 16:09

Darin Dimitrov