API调用的Angular HttpClient CORS错误
我正在与Angular创建一个基于Web的平台,该平台与Magic Eden API交互(文档: https://api.magiceden> .dev/)。
请记住,这不是我的API,我只是从我的前端打电话。
当我通过Angular HTTP客户端向API拨打API时,我会出现A CORS错误,指出“在请求的资源上不存在'访问访问控制 - 允许孔'标头。”
但是,当我通过Postman进行呼叫时,它可以正常工作,没有任何问题。
我尝试在以下两种方式中添加“访问控制 - 允许孔”标头:
1)
headers = new HttpHeaders().set('Access-Control-Allow-Origin', '**');
getListings(symbol: string)
{
return this.http.get('https://api-mainnet.magiceden.dev/v2/collections/aos/listings?offset=0&limit=18', {headers: httpOptions.headers});
}
const httpOptions = {
headers: new HttpHeaders ({
"Access-Control-Allow-Origin": "**"
})
}
getListings(symbol: string)
{
return this.http.get('https://api-mainnet.magiceden.dev/v2/collections/aos/listings?offset=0&limit=18', httpOptions);
}
我还尝试将Access-Control-Allow-Origin设置为“*”和“ **”,该Origin不起作用。
我还尝试使用Axiom而不是Angular HTTPClient进行呼叫,但我仍然会遇到相同的错误。
有人知道如何处理吗? 任何帮助将不胜感激!
I'm creating a web based platform with Angular that interacts with the Magic Eden API (documentation: https://api.magiceden.dev/).
Please keep in mind this is not my API, I'm merely making calls to it from my front end.
When I make API calls to the API through the Angular HTTP Client I get the a CORS error stating "No 'Access-Control-Allow-Origin' header is present on the requested resource."
However, when I make the calls through Postman it works without any issue.
I have tried adding the 'Access-Control-Allow-Origin' header in the following 2 ways:
1)
headers = new HttpHeaders().set('Access-Control-Allow-Origin', '**');
getListings(symbol: string)
{
return this.http.get('https://api-mainnet.magiceden.dev/v2/collections/aos/listings?offset=0&limit=18', {headers: httpOptions.headers});
}
const httpOptions = {
headers: new HttpHeaders ({
"Access-Control-Allow-Origin": "**"
})
}
getListings(symbol: string)
{
return this.http.get('https://api-mainnet.magiceden.dev/v2/collections/aos/listings?offset=0&limit=18', httpOptions);
}
I have also tried setting the Access-Control-Allow-Origin to "*" and "**" which didn't work.
I also tried making the calls with Axiom instead of the Angular HttpClient and I still get the same error.
Does anyone know how to approach this?
Any assistance would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在其他端口上运行该应用
try to run the app on a different port like : ng serve --port 4209
为了理解CORS错误,让我们阅读 wikipedia /strong>:
简单单词的技术步骤:
为了允许客户端访问服务器端,您应该在服务器端启用交叉原始权限。
如果您不能在服务器端添加CORS权限,因为您不是API的所有者,并且不能要求API的所有者将您的站点添加到CORS中,则可以制作自己的服务器代理(例如构建.NET Core Core Backend应用等...)。
在.NET Core 6:
请参阅此如何在asp.net core中启用cors 以获取更多示例。
In order to understand CORS errors, lets read what wikipedia writes about CORS:
The technical steps in simple words:
In order to allow the client accessing the server side you should enable a cross-origin permition in the server side.
If you cant add a CORS permition in the server side because you are not the owner of the API and you cant ask the API's owner to add your site to CORS, you can make your own server proxy, (like build a .Net Core backend app etc...).
In .NET Core 6:
See this How to enable CORS in ASP.NET Core for more samples.