Estoy pasando una matriz del servlet a través de la llamada ajax. Pero cuando trato de obtener lo mismo en el lado del servlet, obtengo una excepción de puntero nulo. Intenté usar uno de los ejemplos dados en SO. Ya que soy un novato, ¡cualquier ayuda sería apreciada!
$.ajax({ url : 'insertserv1', type: 'POST', dataType: 'json', data: {tablearray:tablearray} , contentType: 'application/json', mimeType: 'application/json', success : function(data) { alert('Hi'); } });
// to get data from inspection table { var tablearray = []; $("#tab_logic tr.data").map(function (index, tr) { $(this).find('td').each(function(){ var $data = $(this).html(); if($(this).find("select").length > 0) { var $x = $(this).find("select").val(); }else{ var $x = $(this).find("input[type='text']").val(); } tablearray.push($x); }); }); }
String[] tablearray = request.getParameterValues("tablearray[]"); for (int i = 0; i < tablearray.length; i++) { System.out.println(InspTableArray[i]); }
Si he entendido bien, esto es lo que estarías buscando:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JSONObject jObj = null; StringBuilder sb = new StringBuilder(); BufferedReader br = request.getReader(); String str = null; while ((str = br.readLine()) != null) { sb.append(str); } try { jObj = new JSONObject(sb.toString()); //This is your JSON data. } catch (JSONException e) { e.printStackTrace(); } }