Angular发送请求到Spring网关时出现跨域错误
创始人
2024-10-24 18:31:32
0

跨域错误通常是由浏览器的安全机制引起的,可以通过 Spring Gateway 的 CORS 配置来解决。

  1. 在 Spring Gateway 的配置文件 application.yml 中添加如下配置:

spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "*" allowedMethods: - GET - POST - PUT - DELETE allowedHeaders: - Content-Type - Authorization maxAge: 3600

  1. 在 Angular 的请求头中添加 Origin 和 Content-Type 参数:

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Origin': 'http://localhost:4200' }) };

this.http.post('http://localhost:8080/api/example', data, httpOptions) .subscribe(response => { console.log(response); });

其中,'http://localhost:4200' 指的是你的 Angular 项目的域名。

这样就可以解决 Angular 发送请求到 Spring Gateway 时出现的跨域错误了。

相关内容

热门资讯

透视攻略!智星德州菠萝插件官网... 透视攻略!智星德州菠萝插件官网,hhpoker到底可以作弊吗,攻略教程(往昔是有挂);致您一封信;亲...
透视新版!wepoker辅助工... 透视新版!wepoker辅助工具,wepoker免费脚本弱密码,AI教程(起初存在有挂),支持语音通...
透视透视!wepoker底牌透... 透视透视!wepoker底牌透视脚本怎样安装,约局吧如何查看是否有挂,介绍教程(本然是真的有挂);w...
透视游戏!wpk透视脚本,we... WePoker透视辅助版本解析‌,透视游戏!wpk透视脚本,wepoker黑侠破解,wepoke教程...
透视科技!wpk辅助购买,wp... 透视科技!wpk辅助购买,wpk插件辅助,切实教程(一向真的有挂);wpk辅助购买是一款益智类棋牌手...
透视有挂!wepoker透视脚... 透视有挂!wepoker透视脚本苹果,pokemmo辅助器脚本下载,AI教程(原本真的有挂);(需添...
透视脚本!红龙poker脚本,... 透视脚本!红龙poker脚本,德普之星透视辅助软件,曝光教程(总是有挂);支持多人共享记分板与复盘,...
透视辅助!wepoekr底牌透... 透视辅助!wepoekr底牌透视,werplan外卦神器,黑科技教程(其实有挂)科技教程也叫必备教程...
透视软件!wpk透视辅助软件,... 透视软件!wpk透视辅助软件,德州机器人代打脚本,微扑克教程(真是存在有挂);大神普及一款德州ai内...
透视最新!aapoker辅助挂... 透视最新!aapoker辅助挂,wepoker透视底牌,新版2025教程(都是是真的有挂)1、许多玩...