Blazor应用从本地机器上的外部API获取数据
我已经构建了一个相当简单的API,该API在本地计算机上正在IIS上运行。它可以正常工作。然后,我将Ocelot网关放在它的前面,也可以正常工作。我现在有一个应该从此API中获取数据的大发应用程序,但它只是失败了。当此应用程序使用自己的API运行时,它运行良好,因此我知道我的Blazor代码没有错。这是使用Ocelot网关失败的行:
customers = await Http.GetFromJsonAsync<Customer[]>("http://gateway.oracle.local/customers");
如果我只是将该URL粘贴到浏览器中,则可以将数据返回正常。
这是我遇到的错误:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: TypeError: Failed to fetch
System.Net.Http.HttpRequestException: TypeError: Failed to fetch
---> System.Runtime.InteropServices.JavaScript.JSException: TypeError: Failed to fetch
at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__13`1[[OracleWCON_V2.RODataAccess.Models.Customer[], OracleWCON_V2.RODataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at OracleWCON_V2.Client.Pages.Customers.ListCustomers.OnInitializedAsync() in D:\source\repos\OracleWCON_V2_220512\Client\Pages\Customers\ListCustomers.razor.cs:line 27
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
如果我直接使用无OCELOT网关的API的URL,我会遇到完全相同的错误。只是为了重新开始,如果我直接在浏览器中使用这些URL,我会得到我期望的结果。
事实是类型错误会导致我认为我的客户类中的一个字段对于返回的数据是不正确的,但是我从API复制了类,所以我看不出这是真的,但是我站立要纠正。
任何建议都非常感激。
I have built a fairly simple API, which is running on IIS on my local machine. It works fine. I then put an Ocelot gateway in front of it, which also works fine. I now have a Blazor app that is supposed to get its data from this API, but it is just failing. When this app runs using its own API it runs fine, so I know there is nothing wrong with my Blazor code. Here is the line that fails using the Ocelot gateway:
customers = await Http.GetFromJsonAsync<Customer[]>("http://gateway.oracle.local/customers");
If I just paste that URL into my browser, it returns the data fine.
Here is the error I am getting:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: TypeError: Failed to fetch
System.Net.Http.HttpRequestException: TypeError: Failed to fetch
---> System.Runtime.InteropServices.JavaScript.JSException: TypeError: Failed to fetch
at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__13`1[[OracleWCON_V2.RODataAccess.Models.Customer[], OracleWCON_V2.RODataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at OracleWCON_V2.Client.Pages.Customers.ListCustomers.OnInitializedAsync() in D:\source\repos\OracleWCON_V2_220512\Client\Pages\Customers\ListCustomers.razor.cs:line 27
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
I get exactly the same error if I use the URL that goes directly to the API without the Ocelot gateway. Just to re-iterate, if I use either of those URLs directly in my browser, I get the results I am expecting.
The fact it is a type error would lead me to think one of the fields in my Customer class was incorrect for the returned data, but I copied the class from the API, so I don't see how that could be true, but I stand to be corrected.
Any advice gratefully received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我的API在HTTP上运行,而我的应用程序正在使用HTTPS,因此引起了此问题。这导致我的浏览器阻止了该应用程序混合内容,一些HTTP和一些HTTP。由于证书问题,我仍然没有工作,但至少我陷入了原始问题的底部。
This issue was caused because my API was running on HTTP, whilst my App was using HTTPS. This caused my browser to block the app as it had mixed content, some HTTP and some HTTPS. I still don't have this working due to certificate issues, but at least I got to the bottom of the original problem.