gpt4 book ai didi

angular - Angular7 中的 CORS 策略阻止了 origin 'http://localhost:4200'

转载 作者:行者123 更新时间:2023-12-03 07:32:52 26 4
gpt4 key购买 nike

我要使用http://5.160.2.148:8091/api/trainTicketing/city/findAll在我的 Angular 项目中休息以获得城市。
我在我的项目中使用了 7.2.15 版的 angular。
当使用 httpClient 获取此 url 时抛出以下错误:

 Access to XMLHttpRequest at 'http://5.160.2.148:8091/api/trainTicketing/city/findAll' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

在浏览器和 postman 中输入 url 时正常工作。

为什么 ?

最佳答案

解决方案 1 - 您需要更改后端以接受传入的请求

解决方案 2 - 使用 Angular 代理 see here

Please note this is only for ng serve, you can't use proxy in ng build



注意:它通过 postman 工作的原因是 postman 不会在您的浏览器发送预检请求时发送。

关于angular - Angular7 中的 CORS 策略阻止了 origin 'http://localhost:4200',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com