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

1,114 阅读1分钟

报错:Access to XMLHttpRequest at 'http://localhost:8080/user/get' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

最近在写一个SpringBoot+Mybatis+Ajax的电商秒杀项目遇到bug,

  • 这是一个跨域请求的错误,程序启动的时候html页面是请求本地域名,这种请求可以正确的达到服务端,并且也有返回,但是Ajax认定他们的域不同,是不安全的,会报错,并且走不到Ajax里面。
  • 处理方式:在SpringBoot中只要Response时刻返回,所有的域即可
  • SpringBoot提供了简单的注解形式@CrossOrigin

再次开启服务器后运行

这样就不会报错了(大笑)!