Ajax 不接受 CORS(预检)响应

Ajax not accepting CORS(preflight) response

我正在使用 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);
                }
            });

发送预检响应后它就停止工作(不关闭服务器,但它就像挂在那里没有任何响应),如果我尝试请求其他任何东西,它会断开连接并响应正在关注

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 服务部分? 我已经阅读了很多博客和 Whosebug 的答案,但我无法解决这个问题 problem.It 只是在从网络获得预检响应后简单地抛出错误 service.I 不知道为什么?可能是我遗漏了什么,各位专家可以指出。

我在单击按钮时调用此 Ajax 请求,但我忘记了在该单击事件中调用 return。我找到了这个解决方案 here

解决方案 1:您可以 return false on click() 函数

return false ; // for onClick funciton

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

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