Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple parameters in json format to a web service using jquery?

Tags:

json

jquery

I'm trying to execute a asp.net webservice using jquery. When I pass only one input parameter it works fine:

$.ajax({  
    type: "POST",  
    url: url,  
    data: "{'Id1':'2'}",  
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: callback
});  

but if I try to pass multiple parameters it fails

$.ajax({  
    type: "POST",  
    url: url,  
    data: "{'Id1':'2'},{'Id2':'2'}",  
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: callback
});    

Of course, when I try to pass 2 input parameters, I modify the web method so that it takes 2 input parameters.

Any ideas?

like image 386
Anthony Avatar asked Feb 02 '09 15:02

Anthony


People also ask

Can we pass parameters in JSON file?

The transfer of parameters to the script can be done either directly or through a JSON or CSV file.


2 Answers

Found the solution:

It should be:

"{'Id1':'2','Id2':'2'}"

and not

"{'Id1':'2'},{'Id2':'2'}"
like image 102
Anthony Avatar answered Oct 02 '22 23:10

Anthony


This is a stab in the dark, but maybe do you need to wrap your JSON arguments; like say something like this:

data: "{'Ids':[{'Id1':'2'},{'Id2':'2'}]}"

Make sure your JSON is properly formed?

like image 35
Mark W Avatar answered Oct 02 '22 23:10

Mark W