.NET 8 Cors

问题描述 投票:0回答:1

我在 Visual Studio 2022 中有 Blazor Web Assembly 独立 pProject。

enter image description here

enter image description here

现在,当我想使用

HttpClient
并从某个网站获取一些数据时,我收到此错误:

从源“http://localhost:5246”获取“https://tapi.bale.ai/someLink/getUpdates”的访问已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头存在于所请求的资源上。如果不透明响应满足您的需求,请将请求模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

如何解决此 CORS 问题,并添加标头来托管它?

visual-studio asp.net-core cors blazor-webassembly webassembly
1个回答
0
投票

尝试使用

BrowserRequestMode.NoCors
,它在我身边有效。

var request = new HttpRequestMessage(HttpMethod.Get, "https://tapi.bale.ai/someLink/getUpdates");
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
var response = await Http.SendAsync(request);
//var content = await response.Content.ReadAsStringAsync();

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.