首页 > 代码库 > tomcat 7服务器跨域问题解决
tomcat 7服务器跨域问题解决
前段时间测试angularjs,然后,用tomcat的时候,老弹出 No ‘Access-Control-Allow-Origin‘这玩意儿,然后,找了很多帖子,无果,
最后,看了tomcat的配置文档,弄了一个,问题就解决了。
- 打开tomcat安装目录 -> 打开 conf 目录 -> 打开web.xml文件
- 将如下代码复制到web.xml文件中(我是放在 460左右的位置,不要放在最前面和最后面,一定要放中间)
<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>*</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>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
tomcat 7服务器跨域问题解决