使用Angularjs我想访问使用Tomcat 7作为Web服务器的Geoserver服务。但是当我调用此服务时,控制台浏览器会显示以下错误:
XMLHttpRequest cannot load http://127.0.0.1:8080/geoserver/rest/workspaces. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3000' is therefore not allowed access.
所以要添加CORS标头,我在usr / share / geoserver / WEB-INF / 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>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
重新启动服务器后,控制台浏览器显示相同的错误。 所以我将上面的行改为这些行:
<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>
这次错误是:
OPTIONS http://127.0.0.1:8080/geoserver/rest/workspaces
(index):1 XMLHttpRequest cannot load http://127.0.0.1:8080/geoserver/rest/workspaces. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3000' is therefore not allowed access. The response had HTTP status code 403.
响应标头是:
**General**
Remote Address:127.0.0.1:8080
Request URL:http://127.0.0.1:8080/geoserver/rest/workspaces
Request Method:OPTIONS
Status Code:403 Forbidden
**Response Headers**
Content-Length:0
Content-Type:text/plain
Date:Mon, 27 Jul 2015 05:57:17 GMT
Server:Apache-Coyote/1.1
**Request headers**
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, access-control-allow-methods
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:127.0.0.1:8080
Origin:http://127.0.0.1:3000
Referer:http://127.0.0.1:3000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36
我在互联网上阅读了很多解释,但似乎没有任何作用,我不知道我做错了什么。