Reputation: 902
I am sending an Json object to the controller using ajax.
$.ajax({
url: "/cart/add.json",
type: "POST",
data:{ 'json': event.data},
});
and the controller
@RequestMapping(value = "/cart/add.json", method = RequestMethod.POST)
public String addToCartJson(@RequestParam("json") final String jsonString, final Model model,
final BindingResult bindingErrors)
I am getting 400 bad request.
Error: "HTTP Status 400 - Required String parameter 'json' is not present"
Any Inputs? Thanks,In advance
Upvotes: 0
Views: 733
Reputation: 623
Server side want to string parameter but client side send to object. JSON.stringfy will be used converts object to string.To bind a json you will need to create a single class holding all your parameters and use the @RequestBody annotation instead of @RequestParam.
Ajax method is rewriting:
$.ajax({
url: "/cart/add.json",
type: "POST",
processData: false,
data: JSON.stringify({
"json": event.data
}),
Controller action part:
@RequestMapping(value = "/cart/add", method = RequestMethod.POST)
public void addEvent(@RequestBody EventData eventData){
}
Upvotes: 2
Reputation: 467
You are not passing the parameter json, you can try the below;
$.ajax({
url: "/cart/add.json",
type: "POST",
data:{ 'json=':event.data}
});
Upvotes: 1