jQuery ajax应用程序和tomcat服务器的CORS实现/启用?

时间:2016-05-12 08:43:57

标签: jquery ajax tomcat cors cross-domain

尽管有数百个关于CORS的主题,我找不到我想要的东西,或者没有什么能帮助我解决我的问题。

我的情况是,

var Request = '<?xml version="1.0" encoding="UTF-8"?> \
                    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> \
                      <SOAP-ENV:Body> \
                        <TicorRequest xmlns="urn:Ticor" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ServiceName="Quote_Gen"> \
                          <WorkDocuments> \
                            <Premium_details id="Premium_details_id_1"> \                            
                              <quote_components id="Quote_components_id_1"> \
                                <AAC_Membership>400000</AAC_Membership> \
                                <Adjustment_fee>2000</Adjustment_fee> \     
                              </quote_components> \
                            </Premium_details> \                            
                          </WorkDocuments> \
                        </TicorRequest> \
                      </SOAP-ENV:Body> \
                    </SOAP-ENV:Envelope>';

var TicorService = $.ajax({
                            type: "POST",
                            processData: false,
                            url: "http://localhost:8022/axis/services/Ticor",
                            data: Request,
                            beforeSend: function(xhr){xhr.setRequestHeader('SOAPAction', 'urn:Ticor'); },                                                 
                            xhrFields: { withCredentials: false },          
                            contentType: "text/xml; charset=\"utf-8\"",
                            crossDomain: true,                          
                            dataType: "xml",
                            success: function(data, status, req)
                            {
                              alert(data);
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown)
                            {
                              alert(errorThrown);
                            },
                            complete: function(XMLHttpRequest, textStatus)
                            {                           

                            }
                    });
  • 但我在Chrome中收到以下错误消息。
  

XMLHttpRequest无法加载http://localhost:8022/axis/services/Ticor。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:8011”访问。 响应的HTTP状态代码为200

  • 然后我尝试在Tomcat服务器(7.0.42)的web.xml文件中添加以下内容。
    <filter>
      <filter-name>CorsFilter</filter-name>
      <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
      <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
      </init-param>
      <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
      </init-param>
      <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>true</param-value>
      </init-param>
      <init-param>
        <param-name>cors.preflight.maxage</param-name>
        <param-value>10</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  • 添加上述内容后,我收到相同的错误消息,但响应的HTTP状态代码为403
你能告诉我这里我缺少什么吗?还是其他选择?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并发现当在chrome中的localhost上请求不同的源来源时总会出现此错误。我尝试在IE中运行,然后它工作正常。我以前在chrome中运行的解决方案是禁用CORS - Chrome上的安全性:

  

RUN - &gt; Chrome.exe --disable-web-security

这是一个参考,表明chrome不支持localhost用于CORS请求,请参阅链接:https://bugs.chromium.org/p/chromium/issues/detail?id=67743

另见stackoverflow帖子:Deadly CORS when http://localhost is the origin

答案 1 :(得分:0)

您的javascript执行xhr.setRequestHeader('SOAPAction', 'urn:Ticor');发送标题SOAPAction。但是这个不在 cors.allowed.headers 中。您必须在web.xml中修改过滤器配置:

<init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,Accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,SOAPAction</param-value>
</init-param>

更新

将参数值accept更改为Accept