尽管有数百个关于CORS的主题,我找不到我想要的东西,或者没有什么能帮助我解决我的问题。
我的情况是,
我有一个在http://localhost:8011上运行的应用程序需要使用另一台服务器上运行的服务http://localhost:8022
我使用jQuery(1.7.1)ajax来调用其他服务器中的服务,
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)
{
}
});
XMLHttpRequest无法加载http://localhost:8022/axis/services/Ticor。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:8011”访问。 响应的HTTP状态代码为200 。
<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>
答案 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
。