[英]Get two array on different variable using json from servlet in jquery
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=?;
});
});
發送包含兩個數組值的JSON對象,並將每個數組值設置為所需的JavaScript變量。
雖然您正在編寫代碼,就像您正在進行同步調用一樣,這可能會導致問題。
將兩個數組放在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;
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.