Ajax不接受CORS(预检)响应

时间:2016-08-12 08:07:32

标签: ajax web-services cors http-post preflight

我正在使用simpleCORSFilter.java来处理CORS请求,这对于其他请求工作正常但不知何故,它不适用于AJAX POST请求。

simpleCORSFilter.java

@Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,X-Requested-by, Content-Type, Accept,Session");
        response.setHeader("Access-Control-Expose-Headers", "Location");
        chain.doFilter(req, res);
    }

Ajax请求

$.ajax({
        url: url,
        dataType: 'json',
        type: 'POST',
        contentType: 'application/json; charset=UTF-8',
        crossDomain: true,
        data: JSON.stringify(data),
        success: function (data) {
                    console.log(' data : '+data);

                },
        error: function(jqXHR, textStatus, errorThrown) {
                    console.log('jqXHR:');
                    console.log(jqXHR);
                    console.log('textStatus:');
                    console.log(textStatus);
                    console.log('errorThrown:');
                    console.log(errorThrown);
                }
            });

发送预检响应后,它就会停止工作(不关闭服务器,但就像挂在那里没有任何响应),如果我尝试请求其他任何东西,它会破坏连接并响应跟随< / p>

Caused by: java.io.IOException: An established connection was aborted by the software in your host machine

服务器日志:

----------------------------
ID: 1
Address: http://localhost:9092/services/searchCMS/
Encoding: UTF-8
Http-Method: OPTIONS
Content-Type: 
Headers: {Accept=[*/*], accept-encoding=[gzip, deflate, sdch], accept-language=[en-US,en;q=0.8], access-control-request-headers=[content-type], access-control-request-method=[POST], connection=[keep-alive], Content-Type=[null], dnt=[1], host=[localhost:9092], origin=[http://localhost:3000], referer=[http://localhost:3000/report], user-agent=[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36]}
--------------------------------------
2016-08-12 09:50:48.912  INFO 6408 --- [nio-9092-exec-1] o.a.c.interceptor.LoggingOutInterceptor  : Outbound Message
---------------------------
ID: 1
Response-Code: 200
Content-Type: text/xml
Headers: {Allow=[POST, GET, PUT, OPTIONS, HEAD], Date=[Fri, 12 Aug 2016 07:50:48 GMT], Content-Length=[0]}
--------------------------------------

我不明白问题的确切位置,Ajax部分或Web服务部分? 我已经阅读了这么多的博客和stackoverflow答案,但我无法解决这个问题。它只是在从Web服务获得预检响应后抛出错误。我不知道为什么?可能是我遗失了一些东西,专家可以指出。

1 个答案:

答案 0 :(得分:0)

我在点击按钮时调用了这个Ajax请求,但我忘了返回该click事件。我找到了这个解决方案here

解决方案1:您可以在click()函数

上返回false
return false ; // for onClick funciton

解决方案2:您可以阻止使用默认值

e.preventDefault();       //  e is the onclick event parameter