前后端跨域问题汇总
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
解决办法1-前端Vue
// 网关地址 const GATEWAY_HOST = process.env.GATEWAY_HOST || '127.0.0.1' const GATEWAY_PORT = process.env.GATEWAY_PORT || '9180' // 转发配置 module.exports = { proxyList: { '/api': { target: 'http://' + GATEWAY_HOST + ':' + GATEWAY_PORT, changeOrigin: true, pathRewrite: { '^/api': '/api' } } } }
解决办法2-后端Java
SpringBoot2.x版本之后的解决跨域问题:
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedHeaders("*") .allowedMethods("*") .maxAge(30 * 1000); } }
解决办法3-Nginx
location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; if ($request_method = 'OPTIONS') { return 204; } }
参考文章:https://segmentfault.com/a/1190000012550346
相关阅读:
1、被爱情伤的句子-被爱情伤了,所以学会了写情感文章,但是现在家里逼婚怎么办_IT博客
2、盘古神话故事的文章-关于盘古的故事_IT屋博客_IT博客
3、差点又相信爱情的句子-我想问一个很简单而又困惑的问题,你们还相信爱情吗_IT屋博客_IT博客为什么_IT屋博客_IT博客
文章声明:以上内容(如有图片或视频在内)除非注明,否则均为小星空博客原创文章,转载或复制请以超链接形式并注明出处。
本文作者:小星空本文链接:https://xxkblog.cn/post/5622.html
还没有评论,来说两句吧...