RJ.*_*RJ. 6 jquery xmlhttprequest http-headers
我有一个简单的jquery ajax调用休息服务.我将contentType设置为"application/json",其余资源配置为接受" MediaType.APPLICATION_JSON ".这是一个POST方法.使用此设置,我收到" 不支持的媒体类型 "错误.
标题信息 在请求标头中显示 "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) {
//...
// ...
//...
}
Run Code Online (Sandbox Code Playgroud)
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);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
请求标题
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
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,我能够以这种方式解决它(参见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);
}
});
Run Code Online (Sandbox Code Playgroud)
在 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");
// ...
// ...
}
Run Code Online (Sandbox Code Playgroud)
结论
看起来您可能正遭受抽象漏洞的困扰。请参阅此响应: JQuery's getJSON() not set Accept header Correctly?
如果您正在进行跨域调用,那么由于 jQuery 从您那里抽象调用的方式,您似乎无法设置接受标头。
不过,您确实说服务器看到了正确的接受标头。这可能表明存在不同的问题。