Reputation: 1166
I am trying to send data to Spring controller but i am recieving 400 badrequest error in browser console. here is my code:
javascript:
function insertDiscussion(title, content,tags) {
$.ajax({
async:false,
url: 'save',
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'json',
data: {
"title": title,
"content": content,
"tags":tags
},
success: function(data) {
generateNoty(data, "warning");
}
});
}
Controller:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveDiscussion(
@RequestParam String title,
@RequestParam String content, @RequestParam(value="tags") String[] tags) {
return "hello";
}
Example is working if we dont send array. When i am trying to send array its giving 400 Bad Request error.
If I access same controller with links its working fine but its not working with jquery ajax. did i missed anything?
Upvotes: 1
Views: 5090
Reputation: 35961
You're expecting @RequestParam
(POST
/GET
parameter) but sending JSON
as request body. You need to use @RequestBody
instead.
Try
public String saveDiscussion(@RequestBody Map json) {
return "hello";
}
See also:
Upvotes: 3