0

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;

});
}); 

working fine, I get the complete array on getdata

Write Two

 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);

How can I get two array on different variable

$(document).ready(function() {
var getdata1;
var getdata2;
$.post("GetItemCode", function(data) {
    getdata1=?;
    getdata2=?;

});
}); 

2 Answers 2

1

Put the two arrays in a JSONObject and retrieve it in 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()
);

Then in Javascript you retrieve it as a property of the returned object.

$(document).ready(function() {
    var getdata1;
    var getdata2;
    $.post("GetItemCode", function(data) {
        getdata1 = data.codeList;
        getdata2 = data.nameList;
    });
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

Send a JSON object containing two array values and set each one to the desired JavaScript variable.

Although you're writing the code like you're making a synchronous call, which will likely lead to problems.

1 Comment

Create a new JSONObject. put the two arrays into it. Use whatever names you put them as to extract them in your success callback. At least try to show some effort in understanding the APIs you're using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.