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

和 here is the web method signature

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

This works fine.

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

新的Web方法如下所示

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

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

EDIT:

以下两种语法有效

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

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

where filter 和 locale are local variables

推荐答案

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

$.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在 comments 部分所建议的,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
});

Jquery相关问答推荐

formData.append 来自不同输入文件的两个文件

获取单选按钮组的值

使用 JQuery 清除下拉列表

jQuery 中的 $this 与 $(this)

对混合的字母/数字数组进行排序

如何使用 jQuery 清空输入值?

如何在jQuery中 Select 具有特定ID的所有元素?

jQuery .get 错误响应函数?

可以在删除类时反转css动画吗?

JSON 到字符串变量转储

javascript 正则表达式用于包含至少 8 个字符、1 个数字、1 个大写和 1 个小写的密码

blueimp 文件上传插件中的 maxFileSize 和 acceptFileTypes 不起作用.为什么?

使用 JQuery 获取触发事件的元素的类

移动 chrome 在滚动时触发调整大小事件

jQuery 如果 div 包含此文本,则替换该部分文本

虽然未定义变量 - 等待

使用 JQuery 激活 bootstrap 选项卡

5秒后jQuery自动隐藏元素

如何使锚链接不可点击或禁用?

为什么我应该创建异步 WebAPI 操作而不是同步操作?