如何使用 jquery 将 json 格式的多个参数传递给 Web 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/503627/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:14:43  来源:igfitidea点击:

How to pass multiple parameters in json format to a web service using jquery?

jqueryjson

提问by Anthony

I'm trying to execute a asp.net webservice using jquery. When I pass only one input parameter it works fine:

我正在尝试使用 jquery 执行 asp.net webservice。当我只传递一个输入参数时,它工作正常:

$.ajax({  
    type: "POST",  
    url: url,  
    data: "{'Id1':'2'}",  
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: callback
});  

but if I try to pass multiple parameters it fails

但是如果我尝试传递多个参数它会失败

$.ajax({  
    type: "POST",  
    url: url,  
    data: "{'Id1':'2'},{'Id2':'2'}",  
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: callback
});    

Of course, when I try to pass 2 input parameters, I modify the web method so that it takes 2 input parameters.

当然,当我尝试传递 2 个输入参数时,我修改了 web 方法,使其接受 2 个输入参数。

Any ideas?

有任何想法吗?

回答by Anthony

Found the solution:

找到了解决办法:

It should be:

它应该是:

"{'Id1':'2','Id2':'2'}"

and not

并不是

"{'Id1':'2'},{'Id2':'2'}"

回答by Mark W

This is a stab in the dark, but maybe do you need to wrap your JSON arguments; like say something like this:

这是黑暗中的刺,但也许你需要包装你的 JSON 参数;像这样说:

data: "{'Ids':[{'Id1':'2'},{'Id2':'2'}]}"

Make sure your JSON is properly formed?

确保您的 JSON 格式正确?

回答by Mandoleen

i have same issue and resolved by

我有同样的问题并通过以下方式解决

 data: "Id1=" + id1 + "&Id2=" + id2

回答by TonBR

I think the best way is:

我认为最好的方法是:

data: "{'Ids':['2','2']}"

To read this values Ids[0], Ids[1].

要读取此值 Ids[0]、Ids[1]。