I would like to send JavaScript array to servlet using jQuery $.ajax
.
var json=[1,2,3,4];
$.ajax({
url:"myUrl",
type:"POST",
dataType:'json',
success:function(data){
// codes....
},
data:json
});
When I use
request.getParameter("json");
request.getParameterValues("json");
It returns null.
How can I access the values?
Send array as value of JS object so you end up as {json:[1,2,3,4]}
.
var json=[1,2,3,4];
$.ajax({
url:"myUrl",
type:"POST",
dataType:'json',
data: {json:json},
success:function(data){
// codes....
},
});
In servlet, you need to suffix the request parameter name with []
.
String[] myJsonData = request.getParameterValues("json[]");
jQuery appends them in order to be friendly towards weak typed languages like PHP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With