解决跨域问题

x33g5p2x  于2022-02-12 转载在 其他  
字(0.8k)|赞(0)|评价(0)|浏览(514)

解决跨域问题

记录一个跨域问题,之前一直是在后端解决跨域的,但是今天出了个问题,所以记录一下。

问题详情

  1. Access to XMLHttpRequest at 'localhost:8443/api/user/login' from origin 'http://localhost:8080' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

问题复现

后端代码

  1. @Configuration
  2. public class CroConfig implements WebMvcConfigurer {
  3. /**
  4. * 解决跨域问题
  5. *
  6. * @param registry
  7. */
  8. @Override
  9. public void addCorsMappings(CorsRegistry registry) {
  10. registry.addMapping("/**")
  11. .allowedOriginPatterns("*")
  12. .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
  13. .allowCredentials(true)
  14. .maxAge(3600)
  15. .allowedHeaders("*");
  16. }
  17. }

前端配置

  1. axios.defaults.baseURL = "localhost:8443/api"

解决方式

在前端配置baseURL的时候需要加上协议

  1. axios.defaults.baseURL = "http://localhost:8443/api"

相关文章