【发布时间】:2015-05-01 07:51:14
【问题描述】:
我正在使用使用 Spring/REST 构建的 API。为了启用 CORS,我使用了 Apache CORS 过滤器。当我尝试使用另一个域向我的 API 发出请求时,我收到了这个错误:
XMLHttpRequest 无法加载。 “Access-Control-Allow-Origin”标头包含多个值“http://localhost:8000, *”,但只允许一个。
我的 CORS 过滤器配置:
<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,Authorization,user,client,lang</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>
我使用 restAngular 执行 API 请求的部分代码:
hjcServices.factory('ParamService', function (Restangular) {
var initParamResource = Restangular.one('param', 'hjc.param.init');
return {
getParam: function () {
return initParamResource.getList("",{},{'user':'joueur','client':'hjc','lang':'fr'});
}
};
});
使用 chrome 浏览器的请求标头/答案:
Remote Address:54.68.34.235:8080
Request URL:http://ec2-54-68-34-235.us-west-2.compute.amazonaws.com:8080/hjc-rest-dev/param/hjc.param.init
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
client:hjc
Connection:keep-alive
Host:ec2-54-68-34-235.us-west-2.compute.amazonaws.com:8080
lang:fr
Origin:http://localhost:8000
Referer:http://localhost:8000/
user:joueur
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:x-requested-with
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:http://localhost:8000
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:Access-Control-Allow-Origin,Access-Control-Allow-Credentials
Access-Control-Max-Age:3600
Content-Length:583
Content-Type:application/json;charset=UTF-8
Date:Sat, 28 Feb 2015 17:48:42 GMT
Server:Apache-Coyote/1.1
API 在使用 curl 时有效,但在使用 Restangular 时无效。
【问题讨论】:
-
欢迎来到 StackOverflow。您是否尝试在 StackOverflow 上搜索包含相同错误消息的类似问题?
-
我做了,但没有找到任何有用的解决方案。
-
如果您尝试过某事但没有成功,最好在帖子正文中描述您的失败努力及其结果。
标签: apache cors restangular