Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将多个参数传递给jQuery ajax调用_Asp.net_Jquery_Ajax - Fatal编程技术网

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

Asp.net 将多个参数传递给jQuery ajax调用,asp.net,jquery,ajax,Asp.net,Jquery,Ajax,我有以下jquery代码来调用aspx页面中的webmethod $.ajax({ type: "POST", url: "popup.aspx/GetJewellerAssets", contentType: "application/json; charset=utf-8", data: '{"jewellerId":' + filter + '}', dataType: "json", success: AjaxSucceeded,

我有以下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)
{
}
如何更改客户端代码以成功调用此新方法签名

编辑:

以下两个语法起作用

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


其中filter和locale是局部变量

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

data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}',
 $.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
                });

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

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

更新:

正如@Alex在评论部分中所建议的,ASP.NET PageMethod希望参数在请求中采用JSON编码,因此应在数据哈希上应用
JSON.stringify

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

有没有其他人注意到json字符串/对象在除David Hedlund之外的所有答案中都是无效的?:)


JSON对象必须按以下方式格式化:{“key”:(“value”| 0 | false)}。此外,将其写成字符串所需的时间比字符串化对象要少得多…

这一切都与您传递的数据有关;必须正确格式化字符串。
$.ajax({
    type: 'POST',
    url: 'popup.aspx/GetJewellerAssets',      
    data: "jewellerId=" + filter+ "&locale=" +  locale,  
    success: AjaxSucceeded,
    error: AjaxFailed
});
如果传递的是空数据,那么数据:{}将起作用。 但是,对于多个参数,它必须正确格式化 e、 g

数据:dataParam


最好的理解方法是使用带有适当消息参数的错误处理程序,以便了解详细的错误

我使用json成功地传递了多个参数

data: "{'RecomendeeName':'" + document.getElementById('txtSearch').value + "'," + "'tempdata':'" +"myvalue" + "'}",

只需添加[这一行在Asp.net中完美工作,并在jason中查找web控件字段,例如:]

data:“{LocationName:”+document.getElementById(“”).value+”,AreaID:“+document.getElementById(“”).value+”}”,

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

data: '{"jewellerId":"' + filter + '","locale":"' + locale + '"}'
如果用户错误地输入特殊字符,如单引号或双引号 由于字符串错误,ajax调用失败

使用下面的方法调用Web服务,不会出现任何问题

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


data: JSON.stringify(parameter)
上面的参数是javascript对象的名称,并在将其传递给ajax调用的数据属性时将其字符串化

            data: JSON.stringify({ "objectnameOFcontroller": data, "Sel": $(th).val() }),

控制器对象名称

考虑使用
JSON.stringify(myObject)
从javascript对象创建JSON字符串,以防以后要将参数分组到类中。感谢回复-但是,当我这样尝试时,会得到http状态500。有什么想法吗?甚至如何调试它?web方法中的断点从不获取新代码,如下$.ajax({type:'POST',url:'popup.aspx/GetJewellerAssets',contentType:'application/json;charset=utf-8',数据:{jewellerId:filter,locale:'en-US',数据类型:'json',成功:ajaxSuccessed,错误:AjaxFailed});要进行调试,首先查看FireBug服务器的确切响应是什么,然后在web服务方法中放置一个断点,看看是否达到了该断点。web方法中的断点永不命中FireBug显示:“消息”:“无效的JSON原语:jewellerId.”,“StackTrace”:“在System.Web.Script.Serialization,所以我猜json的语法是不正确的:“{”jewellerId:“+filter+”,“locale:“en”}”,(显然,我不会将区域设置硬编码为en,但这是有效的语法。这对我来说适用于MVC 3。我无法理解Darin的工作方式-也许这是MVC 3的事情。
data:JSON.stringify({jewellerId:filter,locale:locale})
是我找到的最好的方法,谢谢@chriscai如果你像我一样是一个悲伤的灵魂,你可能会被困在这个问题上好几个小时。当使用
JSON.stringify
和object literal时,你必须包括参数名和冒号,所有的参数名都被包装在
{}
大括号内。使用
JSON.stringify(objectLiteral)
不起作用。方法签名,如
[WebMethod][ScriptMethod(UseHttpGet=true)]公共静态字符串TestIBAN(字符串ccc)
?也可以使用类型:GET工作?或者可以使用数组在jason.stringyfy(数组)中传递数据。在这种情况下,在服务器端读取
数据中的属性时,webmethod签名会是什么样子?在未进行“清理”之前,切勿将用户数据传递给参数。
var parameter = {
       jewellerId: filter,
       locale : locale 
};


data: JSON.stringify(parameter)
            data: JSON.stringify({ "objectnameOFcontroller": data, "Sel": $(th).val() }),