我的 ajax 请求被发送但从未到达我的控制器,我收到 415 错误 ()
Ajax 请求
function likeAjax(mId) {
var data = {}
data["id"] = mId;
$.ajax({
type : "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
url : "/likeMessage",
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
beforeSend :(xhr)=>{
xhr.setRequestHeader(csrfheader,csrftoken); //for Spring Security
},
success : (data) =>{
console.log("SUCCESS : ", data);
alert(data);
},
error : (e)=> {
console.log("ERROR: ", e);
},
done : function(e) {
alert("DONE : " + e.toString());
console.log("DONE");
}
});
控制器
@ResponseBody()
@RequestMapping(value = "/likeMessage", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public AjaxResponseBody likeWithAjax(@RequestBody() AjaxRequestBody request) {
AjaxResponseBody result = new AjaxResponseBody();
//some logic
return result
}
AjaxRequestBody 和 AjaxResponseBody 类
private class AjaxRequestBody{
int id;
}
private class AjaxResponseBody{
String message;
}
我确定我遗漏了一些明显的东西,但我似乎无法弄清楚这一点。
谢谢您的帮助