首页 > 技术文章 > 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法

imfanqi 2015-08-07 00:59 原文

问题描述

通过ajax访问Web服务器(Tomcat7.0.42)中的json web service接口的时候,报以下跨域问题:

XMLHttpRequest cannot load http://localhost:8080/get-employees-by-name/name/admin. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

解决办法

1、下载cors-filter-<version>.jar和java-property-utils-<version>.jar两个jar文件,并将其放在web服务器的classpath路径下,例如tomcat的lib。

cors-filter-2.4.jar java-property-utils-1.9.1.jar

2、在web.xml中添加CorsFilter过滤器

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>

3、重启Web服务器即可。

 

参考资料

1、http://stackoverflow.com/questions/17267023/tomcat-7-cors-filter

2、http://software.dzhuvinov.com/cors-filter-installation.html

推荐阅读