【发布时间】:2012-04-19 04:15:16
【问题描述】:
我正在对休息服务进行简单的 jquery ajax 调用。我将 contentType 设置为“application/json”,其余资源配置为接受“MediaType.APPLICATION_JSON”。这是一个 POST 方法。 使用此设置,我收到“Unsupported Media Type”错误。
标题信息显示 请求头中的“Content-Type application/json;charset=UTF-8”
响应显示:状态报告:不支持的媒体类型 服务器拒绝了此请求,因为请求实体的格式不受所请求方法的请求资源支持(不支持的媒体类型)。
请提供一些解决此问题的建议。
这里是sn-p的代码:
休息资源
@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 中显示的标题
响应标头
Content-Length 1117
Content-Type text/html;charset=utf-8
Date Thu, 05 Apr 2012 09:44:45 GMT
Server Apache-Coyote/1.1
请求标头
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
【问题讨论】:
标签: jquery http-headers xmlhttprequest