将多个参数传递给jQuery ajax调用

Chr*_*sCa 97 asp.net ajax jquery

我有以下jquery代码来调用aspx页面中的webmethod

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

这是web方法签名

[WebMethod]
public static string GetJewellerAssets(int jewellerId)
{
Run Code Online (Sandbox Code Playgroud)

这很好用.

但是现在我需要将两个参数传递给web方法

新的Web方法看起来像这样

[WebMethod]
public static string GetJewellerAssets(int jewellerId, string locale)
{
}
Run Code Online (Sandbox Code Playgroud)

如何更改客户端代码以成功调用此新方法签名?

编辑:

以下2种语法有效

data: '{ "jewellerId":' + filter + ', "locale":"en" }',
Run Code Online (Sandbox Code Playgroud)

和

data: JSON.stringify({ jewellerId: filter, locale: locale }),
Run Code Online (Sandbox Code Playgroud)

其中filter和locale是局部变量

Dar*_*rov 137

不要使用字符串连接来传递参数,只需使用数据哈希:

$.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
});
Run Code Online (Sandbox Code Playgroud)

更新:

正如@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
});
Run Code Online (Sandbox Code Playgroud)

  • 还可以考虑使用`JSON.stringify(myObject)`从javascript对象创建一个JSON字符串,以防您以后要将参数分组到一个类. (12认同)

Dav*_*und 18

data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}',
Run Code Online (Sandbox Code Playgroud)


pix*_*ine 7

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

 $.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
                });
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,在服务器端读取“data”中的属性时,webmethod 签名会是什么样子? (2认同)

Jus*_*day 5

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


小智 5

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

data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}'
Run Code Online (Sandbox Code Playgroud)

如果用户错误地输入了单引号或双引号等特殊字符,则 ajax 调用将因错误的字符串而失败。

使用以下方法调用 Web 服务没有任何问题

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


data: JSON.stringify(parameter)
Run Code Online (Sandbox Code Playgroud)

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