将多个参数传递给jQuery的ajax调用 [英] pass multiple parameters to jquery ajax call

查看:158
本文介绍了将多个参数传递给jQuery的ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的jquery code来调用一个aspx页面的WebMethod

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

和这里的Web方法签名

and here is the web method signature

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

这工作得很好。

但现在我需要两个参数传递到Web方法

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

新的Web方法看起来像这样

the new web method looks like this

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

如何更改客户端code成功地把这种新方法的签名?

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

编辑:

下面的2语法工作

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

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

在这里过滤器和语言环境是局部变量

where filter and locale are local variables

推荐答案

不要使用字符串连接来传递参数,只需使用一个数据散列:

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



更新:

由于在评论部分建议的@Alex,一个ASP.NET PageMethod的预期参数是JSON EN codeD上的要求,所以 JSON.stringify 应上的数据的散列被应用

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

这篇关于将多个参数传递给jQuery的ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆