I have a jQuery ajax call like this:
$.ajax({
type: "post",
url: url,
data: JSON.stringify(formSubmit),
contentType: "application/json",
dataType: "json",
success: function(xhr,status) {
console.log("Return Data:"+xhr.responseText)
});
My Controller looks like this:
@RequestMapping(value = "/doIt", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public MyObject activityPerformed(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
logger.debug("JSON INPUT:"+jsonData);
MyObject o = new MyObject("STUFF");
return o;
}
This throws a 406 error. I can see in my browser that I have this in my response headers:
Content-Type:"text/html;charset=utf-8"
Which I thought the produces part of the mapping should have fixed.
When I remove the "contentType" of the ajax call, I do not get the 406 error, but then the jsonData parameter in URLEncoded. No I assume that I could do a decode, but that, I think, is not "right".
So how do I send non URL encoded JSON to a controller without the contentType: "application/json" in the ajax call?
OR how do I set the contentType of the controller response?