I'm building a Tomcat project, but I need to send a JavaScript Map to the Servlet.
I tried to do this with JSON and Ajax. With a simple array I don't have any problems. But when I send a map on the server side it only prints "[Ljava.lang.String@" and the reference. But not the contents.
This is my code: My map name is ticket, and this is its structure:
ticket[id] = { nombre: nombre, precio:price,cantidad: 1 };
On the client side I call this function to send this Map via Ajax.
First I transform the map to a JSON:
var datos=JSON.stringify(ticket);
And I send the request like this:
$.ajax({
url:"VentaProductos",
type:"POST",
dataType:'json',
data: {datos},
success:function(data){
alert("OK")
},
});
And this is the server side. This is inside the method doPost():
String[] myJsonData = request.getParameterValues("datos");
System.out.println("Servlet"+request.getParameterValues("datos").toString());
if(myJsonData!=null) {
System.out.println("It works");
};
System.out.println
only prints "Servlet [Ljava.lang.String;@320aaecf" only a reference. How can I solve it to receive the contents of this Map? Thanks
I finally changed the Map for a multidimensional array and it works with this:
Client side:
$.ajax({
url:"VentaProductos",
type:"POST",
dataType:'json',
success:function(data){
// codes....
},
data: {json: JSON.stringify(multidimensionalarray)}
});
And server side:
String[] data = request.getParameterValues("json");
I hope that it could help somebody in the future :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.