jquery ajax 休息调用 - 不支持的媒体类型

标签 jquery http-headers xmlhttprequest

我正在对休息服务进行简单的 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");

    // ...
    // ...
} 

结论

关于jquery ajax 休息调用 - 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10028335/

相关文章:

ruby-on-rails - Rails媒体文件流通过send_data或send_file方法接受字节范围请求

PHP file_get_contents() : content truncated from 2147483648 to 2147483647 bytes

http - 当 Date 和 Expires 值都过去时,预期的缓存行为是什么?

excel - 将 Web 浏览器自动化转换为 XmlHTTP 请求

jquery - bxslider/firefox 幻灯片对齐问题

javascript - 使用 RESTful API,如果特定响应 json 级别未定义,我如何不在客户端出错

xmlhttprequest - Office 加载项 : XMLHttpRequest cannot load XXX due to access control checks

java - XMLHttpRequest 状态为 0

javascript - JQuery Tooltipster 隐藏它附加的元素

javascript - 转义用户生成的聊天消息但呈现链接