加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

No 'Access-Control-Allow-Origin' header is p

发布时间:2020-12-17 09:59:51 所属栏目:安全 来源:网络整理
导读:今天在使用Angularjs ajax调用restful接口的时候,遇到了一个跨域问题: XMLHttpRequest cannot load http://localhost: 8080 /***/ v1/rest /***/ ***. No 'Access -Control-Allow-Origin' header is present on the requested resource. Origin 'http ://l

今天在使用Angularjs ajax调用restful接口的时候,遇到了一个跨域问题:

XMLHttpRequest cannot load http://localhost:8080/***/v1/rest/***/***. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:34370' is therefore not allowed access.

我先大概描述一下我的应用场景:
我有一批数据,它是存储在MySQL数据库,然后呢,我用jersey写了一些restful接口,供其他应用来查询数据,在这里,我有一个前台网站,这个网站需要调用那个restful接口,以查询相关数据展现在前端,前端是用Angularjs的$http来请求的,但是在请求的时候,出现了以上的跨域问题。

解决方案(参考网站):

直接找到Tomcat安装目录下的conf/web.xml,然后加入以下代码:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

注意:Apache Tomcat 是从7.0.41版本开始支持CORS,如果是这之前的版本,请查找其他相关文献。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读