错误 415 与 ajax 请求 (Spring)



我的 ajax 请求已发送但从未到达我的主计长,我收到 415 错误 ((

阿贾克斯请求

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;
}

我确定我错过了一些明显的东西,但我似乎无法弄清楚这一点。

谢谢你的帮助

从控制器方法中删除 ResponseBody(( 注释。确保已在控制器类中添加了 RestCOntroller 注释。

Add produce = "application/json" in RequestMapping

最新更新