Servlet
String getCodeList = (new JSONArray(rmsCodeList)).toString();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(getCodeList);
Jquery
$(document).ready(function() {
var getdata;
$.post("GetItemCode", function(data) {
getdata=data;
});
});
工作正常,我在getdata上获得了完整的数组
写两个
String getCodeList = (new JSONArray(rmsCodeList)).toString();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(getCodeList);
String getNameList = (new JSONArray(rmsNameList)).toString();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(getNameList);
如何在不同变量上获取两个数组
$(document).ready(function() {
var getdata1;
var getdata2;
$.post("GetItemCode", function(data) {
getdata1=?;
getdata2=?;
});
});
最佳答案
将两个数组放入 JSONObject
中并在 Javascript 中检索它
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(
new JSONObject()
.put("codeList", new JSONArray(rmsCodeList))
.put("nameList", new JSONArray(rmsNameList)).toString()
);
然后在 Javascript 中将其作为返回对象的属性进行检索。
$(document).ready(function() {
var getdata1;
var getdata2;
$.post("GetItemCode", function(data) {
getdata1 = data.codeList;
getdata2 = data.nameList;
});
});
关于java - 使用 jquery 中 servlet 中的 json 获取不同变量上的两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10805540/