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 ? 
}
Asons
  • 84,923
  • 12
  • 110
  • 165
SandyKrish
  • 222
  • 1
  • 11

1 Answers1

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

Asons
  • 84,923
  • 12
  • 110
  • 165
  • Ok , how can i get this JSON string in servlets ? – SandyKrish Jul 07 '18 at 08:37
  • @SandyKrish Updated with a great link/answer – Asons Jul 07 '18 at 08:42
  • 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 Jul 07 '18 at 13:06
  • @SandyKrish Check the link in my answer, it shows how-to using something like `JSONObject jsonObject = HTTP.toJSONObject(jb.toString());` – Asons Jul 07 '18 at 13:10