0

I have store the textbox's value to the multidimensional array in Javascript named as records (below code) and I need to send these array object to my servlet page. But I have no idea about how to get the complete array data.

Javascript code

function insert(row,col){   
var r = row, c=col; 
var q = new Array(); 
for(i=0; i<r; i++){
    q[i] = new Array(); 
    for(j=0; j<c; j++){
    var pick = "#"+i+j;  // select the id's of textbox 
    q[i][j] = $(pick).val(); // store the textbox value to array
    }
  }
 $.ajax({
    url: 'Insert',
    type: 'post',
    data: {records : q, row: r,field: c },   // need to send the records array
    success: function(result){
        console.log(result);
    }
 });
}

Java code

protected void doGet(HttpServletRequest request, HttpServletResponse 
 response) throws ServletException, IOException {

 PrintWriter out = response.getWriter();
// need to get the javascript array. but HOW ? 
}
2
  • Well, first you need to decide on which HTTP method to use. type: 'post' vs doGet doesn't match. Commented Jul 7, 2018 at 8:06
  • yes ! i know , I call doGet method by doPost. Commented Jul 7, 2018 at 8:34

1 Answer 1

1

You can't send an object like that using Ajax, use JSON.stringify() to create a JSON string, e.g.

data: JSON.stringify({records : q, row: r,field: c }),

And as commented, decide which HTTP method to use, as if Ajax is of type post the servlet won't catch it with a doGet.


Updated

Here is a good answer, showing a doPost in more detail

Sign up to request clarification or add additional context in comments.

4 Comments

Ok , how can i get this JSON string in servlets ?
@SandyKrish Updated with a great link/answer
Thanks its really help full, but i have another, this JSON.stringify() convert the object to string format but i need to convert as array in java, how can i convert . It totally print as string like this OUTPUT : [["HAI","I","AM","SANDY"],["HOW","DO","YOU","DO"]] I need to split this string to array again.
@SandyKrish Check the link in my answer, it shows how-to using something like JSONObject jsonObject = HTTP.toJSONObject(jb.toString());

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.