我正在对休息服务进行简单的 jquery ajax 调用。我将 contentType 设置为“application/json”,其余资源配置为接受“MediaType.APPLICATION_JSON”。这是一个 POST 方法。 通过此设置,我收到“不支持的媒体类型”错误。
标题信息显示 请求 header 中的“Content-Type application/json; charset=UTF-8”
响应显示:状态报告:不支持的媒体类型 服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持(不支持的媒体类型)。
请提供一些解决此问题的指示。
这是代码片段:
休息资源
@POST
@Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
@Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
public Response addPerson(MyJSONObj myObj) {
//...
// ...
//...
}
jquery
$(document).ready(function() { /* put your stuff here */
$("#Button_save").click(function(){
var firstName = $('firstName').val();
var lastName = $('lastName').val();
var person = {firstName: firstName, lastName: lastName};
$.ajax({
url:'http://localhost:8080/sampleApplication/resources/personRestService/',
type: 'POST',
data: person,
Accept : "application/json",
contentType: "application/json",
success:function(res){
alert("it works!");
},
error:function(res){
alert("Bad thing happend! " + res.statusText);
}
});
});
});
FF Firebug 中显示的 header
响应 header
Content-Length 1117
Content-Type text/html;charset=utf-8
Date Thu, 05 Apr 2012 09:44:45 GMT
Server Apache-Coyote/1.1
请求 header
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Content-Length 97
Content-Type application/json; charset=UTF-8
Host localhost:8080
Referer http://localhost:8080/sampleApplication/
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0
X-Requested-With XMLHttpRequest
最佳答案
我遇到了同样的问题,并且我能够以这种方式解决它(请参阅http://www.weverwijk.net/wordpress/tag/jquery/):
$.ajax({
url:'http://localhost:8080/sampleApplication/resources/personRestService/',
type:'POST',
data: JSON.stringify(person),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success:function(res){
alert("it works!");
},
error:function(res){
alert("Bad thing happend! " + res.statusText);
}
});
在 Java 端,我添加了这些(请参阅 Access-Control-Allow-Origin ):
@OPTIONS
public Response testt(@Context HttpServletResponse serverResponse) {
serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
serverResponse.addHeader("Access-Control-Allow-Credentials", "true");
serverResponse.addHeader("Access-Control-Allow-Origin", "*");
serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
serverResponse.addHeader("Access-Control-Max-Age", "60");
return Response.ok().build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
public Response addPerson(MyJSONObj myObj, @Context HttpServletResponse serverResponse)
serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
serverResponse.addHeader("Access-Control-Allow-Credentials", "true");
serverResponse.addHeader("Access-Control-Allow-Origin", "*");
serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
serverResponse.addHeader("Access-Control-Max-Age", "60");
// ...
// ...
}
结论
- JSON 对象会自动传输和转换(更多详细信息请参阅 Configuring JSON for RESTful Web Services)
- POST 提交
- 跨域(同源策略)
- Firefox 可以工作(请参阅@Option 标签)
关于jquery ajax 休息调用 - 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10028335/