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?
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:
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.