1

我正在尝试从 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 的错误。我一直在寻找答案很长时间都无济于事。我知道我可能在这里遗漏了一些简单的东西。任何帮助表示赞赏,提前感谢。

4

1 回答 1

0

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

于 2013-09-23T16:20:21.323 回答