tomcat 7服务器跨域问题解决

admin0条评论 455 次浏览

前段时间测试angularjs,然后,用tomcat的时候,老弹出 No ‘Access-Control-Allow-Origin’这玩意儿,然后,找了很多帖子,无果,

最后,看了tomcat的配置文档,弄了一个,问题就解决了。

  1. 打开tomcat安装目录 -> 打开 conf 目录 -> 打开web.xml文件
  2. 将如下代码复制到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>