0

I am having a slight problem with ajax and Java servlet.

$.ajax({
    url: 'sampleURL',
    type: 'POST',
    data:data,
    success: function(response){ },
    error: function(){}
});

My data object is made like this

{name:'name',vars:[array]}

My vars array exists, and is not empty.

When i try to do this thing in servlet

System.out.println(request.getParameter("name")); //it does print name

But when I try to print parameter vars it is always null.

Where did I go wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rouz
  • 1,247
  • 2
  • 15
  • 37

2 Answers2

0

Well It is a bit weird for this to be needed but if I created object like this

{name:'name',JSON.stringify(vars:[array])}

Param vars is not null anymore and I can use it as JSON

Rouz
  • 1,247
  • 2
  • 15
  • 37
0

in .js(java script)

var variable="test";
$.ajax({
    url: baseUrl + "nameController/test1",
    async: false,
    data: {val: variable},
    dataType: 'html',
    success: function (dat) {
        console.log(dat);
    }
});

you create nameController.java

@RequestMapping(value = "test1", method = RequestMethod.POST)
public @ResponseBody
String checkRoomStatusReservation(@RequestParam(value = "val", required = true) String parse) {
    System.out.println("parse"+parse);
    //value from parse=test
return parse;
}

you can try this

lucky kurniawan
  • 136
  • 2
  • 8