报错:

Access to XMLHttpRequest at 'http://127.0.0.1:8088/user/list' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
xhr.js?b50d:178 GET http://127.0.0.1:8088/user/list net::ERR_FAILED

在学习前后端分离开发的过程中,遇到这个报错是非常正常的,当然也是比较好解决的问题,莫非就是前端项目使用了一个端口,后端项目使用了一个端口,但是其实同一个ip,但是不同端口之间也不能直接获取信息的,那么就要解决它。

先说一下情况,当前我是一个Spring Boot项目,版本是2.x版本。

想要更多了解的话,你可以看一下我三年前写的一篇文章,这样子解决也行,如果是自己学习的话:SpringBoot+JSON+AJAX+ECharts+Fiddler实现前后端分离开发可视化(进阶篇)
直接看菜单部分即可,通过软件进行端口转发,但这其实还是有点麻烦的,而且不够灵活。

Spring Boot项目中,解决非常简单,新建一个配置类,然后用注解注入即可:

@Configuration
public class CorsConfig implements WebMvcConfigurer {public void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedHeaders(CorsConfiguration.ALL).allowedMethods(CorsConfiguration.ALL).allowCredentials(true).maxAge(3600); // 1小时内不需要再预检(发OPTIONS请求)}
}

重启项目,就可以解决了!

当然,如果你还没解决,还有一个常见的错误,那就是,你前端的请求路径写错了,务必要小心!!!


如果是Spring Cloud项目,使用的是Gateway网关,可直接在GatewayApplication加上以下内容即可:

 @Beanpublic CorsWebFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(Boolean.TRUE);config.addAllowedMethod("*");config.addAllowedOrigin("*");config.addAllowedHeader("*");config.setMaxAge(3600L);UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());source.registerCorsConfiguration("/**", config);return new CorsWebFilter(source);}

加上了就可以删掉上面的 CorsConfig了,如果你有加了的话。

报错:跨域问题解决 No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.相关推荐

  1. Vue 跨域请求报错No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    坑是一个一个踩出来的... 报错如下: Access to XMLHttpRequest at 'http://apps.eshimin.com/traffic/gjc/getArriveBase?n ...

  2. 解决springboot跨域问题No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

    文章目录 1.问题描述 2.问题产生 3.解决方案 1. 在WebMvcConfig添加(推荐使用) 2.直接采用SpringBoot的注解@CrossOrigin 1.Controller层在需要跨 ...

  3. 新版本Chrome同源策略、跨域问题处理No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    本文的方法实用于本地调试浏览器的设置,如果想要彻底解决需要使用 CORS(跨域资源共享) [ 相关文章 ] Python 使用 CORS 跨域资源共享解决 flask 服务器跨域问题.浏览器同源策略 ...

  4. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  5. 解决跨域问题:No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. PS:如果遇到 这个问题 Request header field Content-Type is ...

  6. Vue--使用webpack解决跨域问题:No ‘Access-Control-Allow-Origin‘ header is present on the requested resource

    原文网址:Vue--使用webpack解决跨域问题:No 'Access-Control-Allow-Origin' header is present on the requested resour ...

  7. header is present on the requested resource. Origin 'null' is therefore not allowed access.

    No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there ...

  8. vue跨域问题解决:Access to XMLHttpRequest at‘httplocalhost

    前两天遇到了跨域问题,报了Access to XMLHttpRequest at'httplocalhost的错,在网上找了一些资料,我是通过配置vue.config.js.proxy实现的,感觉非常 ...

  9. 解决直接打开 vite +vue3 打包的 HTML 文件报错跨域 from origin ‘null‘ has been blocked by CORS policy

    问题 原因 vite 是直接使用原生 ES6 模块打包的 <script type="module" />(是让浏览器接管了打包程序的部分工作),然而 ES6 模块遵循 ...

最新文章

  1. Ubuntu 安装 Etcd
  2. 剖析ifstream打开含中文路径名文件失败的原因(转)
  3. Spring JDBC-事务方法嵌套调用解读
  4. Maven的Settings.xml配置文件解释
  5. es6 Promise 的应用
  6. TensorFlow HOWTO 2.2 支持向量回归(软间隔)
  7. matplotlib之legend图例和标注(笔记三)
  8. 服务器迁移虚拟化实施方案,虚拟化实施方案
  9. 一样的打游戏,不一样的酷
  10. Windows 未能启动。原因可能是最近更改了硬件或软件。解决此问题的步骤
  11. tomcat ---- 启动,关闭和配置等等
  12. 怎么用谷歌或百度搜自己在csdn内的文章呢?
  13. Android报错之You need to use a Theme.AppCompat theme (or descendant) with this activity.
  14. qmake,make,nmake
  15. 大学本科基于html5毕业设计题目50例
  16. 基于Labview与单片机的多通道温度采集系统设计
  17. 省市县三级数据联动 -Java
  18. C语言源程序作业完成系统,C语言源程序的自动评判系统
  19. 计算机网络实验三—— Cisco Packet Tracer 实验
  20. 转:Excel Web Access Web Part

热门文章

  1. daliu_IT学习Android笔记第一篇--Android是什么?安卓的logo来源?安卓的发展过程?
  2. 11.判断一个人出生了多少天
  3. Oracle针对EMP表的练习题(很有用,值得收藏)
  4. Google浏览器简体中文版下载
  5. 史上最全的Python定量金融三方库汇总
  6. 6.7 【实例】窗口查看器
  7. 2022-2028全球与中国纳米保湿美容仪市场现状及未来发展趋势
  8. nginx过滤HttpHeader的 中划线
  9. Dichotomy专栏:Leetcode:#33 搜索旋转排序数组
  10. border边框线样式