javascript 传递json数组ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17732740/
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
passing json array ajax
提问by user1908568
Trying to pass a array using ajax call.
尝试使用 ajax 调用传递数组。
info = [];
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
data: {info: info, "action": "getPatientRecords"},
url: "/mobiledoc/jsp/ccmr/webPortal/carePlanning/servicePatientmilestoneModal.jsp",
success: function(msg) {
$('.answer').html(msg);
}
});
However when i try to catch it on the server side using : request.getParameter("info"); //Displays null**
但是,当我尝试使用以下方法在服务器端捕获它时: request.getParameter("info"); //显示空**
Also, if i wish to send an array of arrays ? is it possible?
另外,如果我想发送一组数组?是否可以?
I tried using serialize however my IE throws error that serialize : object doesnt support this propertyi Did include jquery lib.
我尝试使用序列化但是我的 IE 抛出了序列化错误:对象不支持这个属性我确实包含了 jquery lib。
回答by Explosion Pills
You can use JSON.stringify(info)
to create a JSON representation of the object/array (including an array of arrays). On the server side you should be able to get the string via getParameter
and then unserialize it from JSON to create constructs JSP can use.
您可以使用JSON.stringify(info)
来创建对象/数组(包括数组数组)的 JSON 表示。在服务器端,您应该能够通过获取字符串getParameter
,然后将其从 JSON 中反序列化以创建 JSP 可以使用的构造。
回答by HIRA THAKUR
Yes,It is Possible to send arrays.
是的,可以发送数组。
var info_to_send = ['hi','hello'];
$.ajax({
type: "POST",
data: {info: info_to_send, "action": "getPatientRecords"},
url: "/mobiledoc/jsp/ccmr/webPortal/carePlanning/servicePatientmilestoneModal.jsp",
success: function(msg) {
$('.answer').html(msg);
}
});
回答by Logan Murphy
You can only provide strings in a request url.
您只能在请求 url 中提供字符串。
You could encode the array like so:
您可以像这样对数组进行编码:
info = JSON.stringify(info);
// results in "['hi', 'hello']"
Then send it to the server, also JSON parse on the server.
然后将其发送到服务器,也在服务器上进行 JSON 解析。
You will need to go to http://www.json.org/to get a Java implementation of JSON parsing.
您需要访问http://www.json.org/以获取 JSON 解析的 Java 实现。