[英]Unsupported Media Type Error in AJAX-Spring MVC
I am trying to pass string to controller use of Ajax Stringify but i got this error 415 (Unsupported Media Type) My Ajax call: 我试图将字符串传递给Ajax Stringify的控制器使用,但出现此错误415(不支持的媒体类型)我的Ajax调用:
$.ajax({
type: "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url: "addSample",
data: JSON.stringify(search), // Note it is important
cache : false,
beforeSend : function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success : function(response) {
console.log("Success: "+ response);
},
error : function(xhr) {
console.log("Sorry, there was a problem! " + xhr.responseText);
},
complete : function() {
console.log("Request complete");
}
});
Passing the String : 传递字符串:
var search = {
" name" : "aa",
"sname": "bb",
"dname" : "cc"
}
My spring controller function: 我的弹簧控制器功能:
@RequestMapping(headers={"Accept=application/json"},value = "addSample", method = RequestMethod.POST)
public @ResponseBody
String addData(HttpServletRequest request,@RequestBody final DemoDTO demoDTO)
{
return "{\"value\":\"true\"}";
}
My DTO: 我的DTO:
public class DemoDTO implements Serializable{
private String name;
private String sname;
private String dname;
//getter setter
}
Please help me for solving this.... 请帮我解决这个问题。
Try to replace this: 尝试替换为:
beforeSend : function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
With this: 有了这个:
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
Also, remove headers={"Accept=application/json"}
from spring controller so it would be as follows: 另外,从spring控制器中删除
headers={"Accept=application/json"}
,如下所示:
@RequestMapping(value = "addSample", method = RequestMethod.POST)
public @ResponseBody
String addData(HttpServletRequest request,@RequestBody DemoDTO demoDTO)
{
return "{\"value\":\"true\"}";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.