【问题标题】:CORS POST not working with Apache on FFCORS POST 无法在 FF 上与 Apache 一起使用
【发布时间】:2012-12-15 14:26:29
【问题描述】:

我正在尝试从 Firefox 到我的 Tomcat 服务器进行 CORS POST 调用。 Tomcat 提供 RESTful 服务。这是我的服务器端返回码:

        ResponseBuilder rb = Response.ok( JSON.serialize(result));

        rb.header("Content-Type", "text/javascript;charset=utf8");
        rb.header("Access-Control-Allow-Origin", "*");
        rb.header("Access-Control-Max-Age", "3628800");
        rb.header("Access-Control-Allow-Methods", "GET,POST");

        return rb.build();

这是我的 AJAX 调用:

var feedQueryPOST = {"param" : someListOfParams, "timeWindow": 36000};

     $.ajax({
        type: 'POST',
        url:"http://xxx.xxx.xxx.xxx:8080/MyWebService/getwebfeed",
        contentType: "text/plain",
        data: feedQueryPOST,
        async: true,
        success:function(json){
            alert("success!");
        },
        error:function(e){
            alert(JSON.stringify(e));
        },
     });

当我进行测试查询时,我使用 RESTClient 来检查标头并返回:

Status Code: 200 OK
Access-Control-Allow-Methods: GET,POST
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3628800
Content-Length: 318
Content-Type: text/javascript;charset=utf8
Date: Mon, 31 Dec 2012 12:02:11 GMT
Server: Apache-Coyote/1.1

但是在我的 javascript 端,回调仍然会出现 readystate = 0 和 status = 0 的错误。我一直在寻找答案很长时间都无济于事。我知道我可能在这里遗漏了一些简单的东西。任何帮助表示赞赏,提前感谢。

【问题讨论】:

  • 尝试设置rb.header("Access-Control-Allow-Headers", "Content-Type");

标签: ajax tomcat cors


【解决方案1】:

不要使用contentType: "text/plain",,而是使用dataType: 'json',应该没问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多