tomcat 7服务器跨域问题解决

前段时间测试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>

 

// 然后, 重启服务器就行了

posted @ 2017-01-01 19:39  0小羊0沉默  阅读(16797)  评论(0编辑  收藏  举报