我使用angular $http调用跨doamin REST Web服务( Spring).请在下面找到代码.
$http({
url: ' http://xxx.yyy.zzz:8080/..../sponsors',
method: 'GET',
headers: { 'Token' : 'abc' }
}).success(function(sponsors){
$scope.sponsorList = sponsors;
}).error(function(sponsors){
alert('failed to get sponsors')
});
我收到以下错误
“对预检请求的响应未通过访问控制检查:请求的资源上没有’Access-Control-Allow-Origin’标头.”
Restful webservices部署在Tomcat-8上.
我在tomcat / conf / web.xml中添加了一个CORS过滤器,如下所示.
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
即使我得到同样的错误.
任何人都可以帮我解决这个问题吗?我错过了客户端的任何标题吗?还有什么需要在服务器端完成吗?
提前致谢
最佳答案 这将基于此 link帮助您
<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>