我有以下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
});
下面是web方法签名
[WebMethod]
public static string GetJewellerAssets(int jewellerId)
{
这可以很好地工作。
但是现在我需要将两个参数传递给web方法
新的web方法如下所示
[WebMethod]
public static string GetJewellerAssets(int jewellerId, string locale)
{
}
如何更改客户端代码以成功调用此新方法签名?
编辑:
以下2个语法起作用
data: '{ "jewellerId":' + filter + ', "locale":"en" }',
和
data: JSON.stringify({ jewellerId: filter, locale: locale }),
其中filter和locale是局部变量
发布于 2009-12-17 01:37:01
data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}',
发布于 2009-12-17 01:37:28
只需向数据对象添加任意数量的属性即可。
$.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
});
发布于 2016-08-25 18:33:08
不要使用下面的方法通过ajax调用发送数据
data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}'
如果用户错误地输入了特殊字符,如单引号或双引号,ajax调用将因字符串错误而失败。
使用下面的方法调用Web服务,不会出现任何问题
var parameter = {
jewellerId: filter,
locale : locale
};
data: JSON.stringify(parameter)
在上面的参数中,是javascript对象的名称,并在将其传递给ajax调用的data属性时将其字符串化。
https://stackoverflow.com/questions/1916309
复制相似问题