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

379285 次浏览
data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}',

只需向数据对象添加所需的属性即可。

 $.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
});

不要使用字符串串联来传递参数,只需使用数据散列:

$.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
});

更新:

正如@Alex 在评论部分所建议的,ASP.NET PageMethod 期望参数在请求中被 JSON 编码,所以 JSON.stringify应该应用于数据散列:

$.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
});

有没有其他人注意到除了 David Hedlund 外,json 字符串/对象在所有答案中都是无效的? :)

JSON 对象必须按以下方式进行格式化: {“ key”: (“ value”| 0 | false)}。另外,将它写出来作为一个字符串需要的远远少于字符串化对象..。

$.ajax({
type: 'POST',
url: 'popup.aspx/GetJewellerAssets',
data: "jewellerId=" + filter+ "&locale=" +  locale,
success: AjaxSucceeded,
error: AjaxFailed
});

它的所有关于数据,你传递; 必须正确格式化的字符串。 如果传递的是空数据,那么 data: {}将起作用。 但是,有多个参数,它必须正确格式化 例如:。

var dataParam = '{' + '"data1Variable": "' + data1Value+ '", "data2Variable": "' + data2Value+ '"' +  '}';

....

数据: dataParam

...

理解错误的最好方法是使用适当的消息参数进行错误处理,以便了解详细的错误。

我使用 json 成功地传递了多个参数

data: "{'RecomendeeName':'" + document.getElementById('txtSearch').value + "'," + "'tempdata':'" +"myvalue" + "'}",

Just to add on [This line perfectly work in Asp.net& find web-control Fields in jason Eg:<%Fieldname%>]

 data: "{LocationName:'" + document.getElementById('<%=txtLocationName.ClientID%>').value + "',AreaID:'" + document.getElementById('<%=DropDownArea.ClientID%>').value + "'}",
    var valueOfTextBox=$("#result").val();
var valueOfSelectedCheckbox=$("#radio:checked").val();


$.ajax({
url: 'result.php',
type: 'POST',
data: { forValue: valueOfTextBox, check : valueOfSelectedCheckbox } ,
beforeSend: function() {


$("#loader").show();
},
success: function (response) {
$("#loader").hide();
$("#answer").text(response);
},
error: function () {
//$("#loader").show();
alert("error occured");
}
});

不要使用以下方法使用 ajax 调用发送数据

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

如果用户错误地输入特殊字符,如单引号或双引号 the ajax call fails due to wrong string.

使用以下方法调用 Web 服务而不会出现任何问题

var parameter = {
jewellerId: filter,
locale : locale
};




data: JSON.stringify(parameter)

在上面的参数中是 javascript 对象的名称,并在将其传递给 ajax 调用的 data 属性时将其字符串化。

            data: JSON.stringify({ "objectnameOFcontroller": data, "Sel": $(th).val() }),

控制器对象名称