Java SpringMVC-Ajax调用返回HTTP状态415–;不支持的媒体类型
我想将表单数据提交给控制器,但即使我在HTTP请求中看到正在发送参数,我还是收到了415 HTTP错误。 下面是我对控制器的JQuery/AJAX调用:Java SpringMVC-Ajax调用返回HTTP状态415–;不支持的媒体类型,java,ajax,http,spring-mvc,Java,Ajax,Http,Spring Mvc,我想将表单数据提交给控制器,但即使我在HTTP请求中看到正在发送参数,我还是收到了415 HTTP错误。 下面是我对控制器的JQuery/AJAX调用: $.ajax({ headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, dataType: "json",
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dataType: "json",
type: 'POST',
url: "calculate",
data : JSON.stringify(data),
success : function(response){
alert("OK");
},
error : function(){
alert("Error");
}
});
@RequestMapping(value = "calculate", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody void calculate (@RequestBody QueryElements queryElements, HttpServletRequest request) throws IOException {
System.out.println("OK");
}
这是我的控制器:
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dataType: "json",
type: 'POST',
url: "calculate",
data : JSON.stringify(data),
success : function(response){
alert("OK");
},
error : function(){
alert("Error");
}
});
@RequestMapping(value = "calculate", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody void calculate (@RequestBody QueryElements queryElements, HttpServletRequest request) throws IOException {
System.out.println("OK");
}
现在发生的事情是,如果我删除控制器中的@RequestBody,我会在调试中看到控制器正在被调用,但显然我需要保留注释
以下是HTTP请求:
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:593
Content-Type:application/json; charset=UTF-8
Cookie:JSESSIONID=AD8FEA2E46709554189D687A7C65929D
Host:localhost:9082
Origin:http://localhost:9082
Referer:http://localhost:9082/cheexport/export?
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
X-Requested-With:XMLHttpRequest
{"bindListregInc":"","bindListregEsc":"","bindListprovInc":"","bindListprovEsc":"","bindListcomInc":"","bindListcomEsc":"","bindListcapInc":"","bindListcapEsc":"","bindListsitInc":"","bindListsitEsc":"","stringaInclusa":"","stringaEsclusa":"","consenso":"","clausola1c":"","sponsor":"","bindListcontrInc":"","bindListcontrEsc":"","bindListcliInc":"","bindListcliEsc":"","nomeContratto":"","customQuote":"","campiHeaderHidden":""}
当您需要JSON时,可能有一个void计算方法。 试着归还一些东西:
@RequestMapping(value = "calculate", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody ResponseEntity<?> calculate (@RequestBody QueryElements queryElements, HttpServletRequest request) throws IOException {
return ResponseEntity.ok("OK");
}
@RequestMapping(value=“calculate”,method=RequestMethod.POST,products=“application/json”)
public@ResponseBody ResponseEntity计算(@RequestBody querylements querylements,HttpServletRequest request)引发IOException{
返回响应。ok(“ok”);
}
当您需要一个JSON时,可能有一个void计算方法。
试着归还一些东西:
@RequestMapping(value = "calculate", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody ResponseEntity<?> calculate (@RequestBody QueryElements queryElements, HttpServletRequest request) throws IOException {
return ResponseEntity.ok("OK");
}
@RequestMapping(value=“calculate”,method=RequestMethod.POST,products=“application/json”)
public@ResponseBody ResponseEntity计算(@RequestBody querylements querylements,HttpServletRequest request)引发IOException{
返回响应。ok(“ok”);
}
在@RequestMapping注释中,您应该添加consumes=“application/json”
这样地:
@RequestMapping(value=“someUrl”,使用={MediaType.APPLICATION\u JSON\u value})
这将告诉Spring方法接受Applicationn/json请求
或者您可以尝试使用@RestController而不是@Controller
还要尝试向JS代码中添加“contentType”。
$.ajax({
url:“/api1/tax”,
数据:JSON.stringify(requestData),
类型:方法,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
setTimeout(可重新加载,globalTimeout);
},
错误:函数(错误){
console.log(错误);
}
}); 在@RequestMapping注释中,您应该添加consumes=“application/json” 这样地: @RequestMapping(value=“someUrl”,使用={MediaType.APPLICATION\u JSON\u value}) 这将告诉Spring方法接受Applicationn/json请求 或者您可以尝试使用@RestController而不是@Controller 还要尝试向JS代码中添加“contentType”。 $.ajax({ url:“/api1/tax”, 数据:JSON.stringify(requestData), 类型:方法, contentType:“应用程序/json;字符集=utf-8”, 成功:功能(数据){ setTimeout(可重新加载,globalTimeout); }, 错误:函数(错误){ console.log(错误); }
}); 不,是一样的。关键是,它甚至没有被控制器捕获,而当我删除@RequestBody时,它没有被捕获,这是一样的。关键是它甚至没有被控制器捕获,当我删除@RequestBody时,它添加了“consumes”属性,我没有任何更改。我可以将此更改保留为“consumes”,并添加到下面的JavaScript代码:$.ajax({url:/api1/tax),data:JSON.stringify(requestData),type:method,contentType:“application/json;charset=utf-8”});我添加了“consumes”属性,而不是头参数put contentTypeI,我没有任何更改。我可以将此更改保留为“consumes”,并添加到下面的JavaScript代码:$.ajax({url:“/api1/tax”,数据:JSON.stringify(requestData),类型:method,contentType:“application/JSON;charset=utf-8”});代替标题参数put CONTENTTYPE,您可以共享QueryElements类吗?并尝试使用Postman或任何RestapicClient发送请求,这将有助于理解问题是在Ajax调用中还是在controllerMay中是JSON。stringify(data)没有以正确的方式发送请求,请尝试硬编码,如
数据:“{”bindListregInc:“,“bindListregEsc”:“bindListprovInc”:“bindListprovEsc”:“bindListcomInc”:“bindListcomEsc”:“bindListcapEsc”:“bindListsitInc”:“bindListsitEsc”:“StringInClusa”:“stringaEsclusa”:“consenso”:“clausola1c”:“赞助商”:“BindListContrecs”:“bindListcliInc”:“bindListcliEsc”:“bindListcliEsc”:“bindListcliEsc”:”nomeContratto:“,”customQuote:“,”campiHeaderHidden:“}”,
通过这种方式,我得到了一个“404错误请求”,您可以共享您的服务器日志吗?扫描您共享的QueryElements类?并尝试使用Postman或任何RestapicClient发送请求,这将有助于了解问题是在Ajax调用中还是在controllerMay中可能是JSON.stringify(数据)如果未以正确的方式发送请求,请尝试硬编码,如数据:{“bindListregInc”:“bindListregEsc”:“bindListprovInc”:“bindListprovEsc”:“bindListcomEsc”:“bindListcomEsc”:“bindListcapInc”:“bindListcapEsc”:“BindListSitic”:“bindListsitEsc”:“stringaEsclusa”:“consenso”:“clausola1c”:”赞助商“:”bindListcontrInc“:”bindListcontrEsc“:”bindListcliEsc“:”nomeContratto“:”customQuote“:”campiHeaderHidden“:”}”,通过这种方式我收到了一个“404错误请求”您可以共享您的服务器日志吗