通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法
2024-10-11 19:33:46
问题描述
通过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
最新文章
- 自己用js实现全屏滚动
- Manacher&#39;s Algorithm 马拉车算法
- iOS 线程间共享资源添加排它锁
- Webpack打包工具实时更新操作(启用观察者模式)
- innerHtml innerText textContent兼容性问题
- iOS SQLite 增删改查的封装(关系型)
- demo16Toast
- 状态压缩dp 入门
- unity3d中切换武器
- js跟随鼠标移动的写法
- Maven快速搭建GUI项目
- HTML5入门必知
- 【JVM虚拟机】(2)---GC 算法与种类
- mysql 使用正则表达式查询
- [转] offsetParent 到底是哪一个?
- laravel command
- C#控件方法及属性大全,望补充
- Java学习个人备忘录之内部类
- 启动hbase输出ignoring option PermSize=128m; support was removed in 8.0告警信息
- Word2Vec词向量(一)