将多个参数传递给 jQuery ajax 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1916309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass Multiple Parameters to jQuery ajax call
提问by ChrisCa
I have the following jquery code to call a webmethod in an aspx page
我有以下 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
});
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
但是现在我需要将两个参数传递给 web 方法
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
以下2种语法有效
data: '{ "jewellerId":' + filter + ', "locale":"en" }',
and
和
data: JSON.stringify({ jewellerId: filter, locale: locale }),
where filter and locale are local variables
其中 filter 和 locale 是局部变量
回答by Darin Dimitrov
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
});
UPDATE:
更新:
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:
正如@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
});
回答by dxh
data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}',
回答by pixeline
simply add as many properties as you need to the data object.
只需向数据对象添加所需数量的属性即可。
$.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
});
回答by Sumit Jambhale
DO not use the below method to send the data using ajax call
不要使用下面的方法使用ajax调用发送数据
data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}'
If by mistake user enter special character like single quote or double quote the ajax call fails due to wrong string.
如果用户错误地输入了单引号或双引号等特殊字符,则 ajax 调用将因错误的字符串而失败。
Use below method to call the Web service without any issue
使用以下方法调用 Web 服务没有任何问题
var parameter = {
jewellerId: filter,
locale : locale
};
data: JSON.stringify(parameter)
In above parameter is the name of javascript object and stringify it when passing it to the data attribute of the ajax call.
上面的参数是javascript对象的名称,并在将其传递给ajax调用的数据属性时对其进行字符串化。
回答by Ariel
Has anyone else noticed that the json string/object is invalid in all answers except for David Hedlund's? :)
有没有其他人注意到除了 David Hedlund 的所有答案中的 json 字符串/对象都是无效的?:)
JSON objects must be formatted in the following manner: {"key": ("value" | 0 | false)}. Also, writing it out as a string requires much less than stringifying the object...
JSON 对象必须按以下方式格式化:{"key": ("value" | 0 | false)}。此外,将其写为字符串所需的时间比将对象字符串化要少得多......
回答by Justinonday
$.ajax({
type: 'POST',
url: 'popup.aspx/GetJewellerAssets',
data: "jewellerId=" + filter+ "&locale=" + locale,
success: AjaxSucceeded,
error: AjaxFailed
});
回答by Milind bilonia
Just to add on [This line perfectly work in Asp.net& find web-control Fields in jason Eg:<%Fieldname%>]
只是添加 [此行在 Asp.net 中完美运行&在 jason 中查找 web-control 字段,例如:<%Fieldname%>]
data: "{LocationName:'" + document.getElementById('<%=txtLocationName.ClientID%>').value + "',AreaID:'" + document.getElementById('<%=DropDownArea.ClientID%>').value + "'}",
回答by Jasbir Rana
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");
}
});
回答by Subodh Patil
Its all about data which you pass; has to properly formatted string. If you are passing empty data then data: {} will work. However with multiple parameters it has to be properly formatted e.g.
它全都与您传递的数据有关;必须正确格式化字符串。如果您传递的是空数据,则 data: {} 将起作用。但是,对于多个参数,它必须正确格式化,例如
var dataParam = '{' + '"data1Variable": "' + data1Value+ '", "data2Variable": "' + data2Value+ '"' + '}';
....
....
data : dataParam
数据:数据参数
...
...
Best way to understand is have error handler with proper message parameter, so as to know the detailed errors.
最好的理解方法是使用带有正确消息参数的错误处理程序,以便了解详细的错误。
回答by user2739266
I successfully passed multiple parameters using json
我使用json成功传递了多个参数
data: "{'RecomendeeName':'" + document.getElementById('txtSearch').value + "'," + "'tempdata':'" +"myvalue" + "'}",