question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor WebAssembly exception while calling REST API calls - WebAssembly.JSException: TypeError: Failed to fetch at WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler.doFetch

See original GitHub issue

I am trying to call REST API call from web Blazor WebAssembly using the following code

var userInfo = new ValidateUserRequest { UserName = UserName, Password = password };
var response = await Http.PostJsonAsync<string>($"{APIBaseUrl}/Security/ValidateUser", userInfo);

The CORS is enabled in the API.

But when running the application, I got the following error.

WebAssembly.JSException: TypeError: Failed to fetch at WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x22fe098 + 0x00988> in <c4a9717a3f954719acec8a5e4e87c87d>:0 at WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x22d6978 + 0x00180> in <c4a9717a3f954719acec8a5e4e87c87d>:0 at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x235f0f8 + 0x00278> in <f29c040f027c48c299bba2e6843ac2e0>:0 at Microsoft.AspNetCore.Components.HttpClientJsonExtensions.SendJsonAsync[T] (System.Net.Http.HttpClient httpClient, System.Net.Http.HttpMethod method, System.String requestUri, System.Object content) <0x22a1c88 + 0x00118> in <393f0d2066e842d38b0e8ac688095cf7>:0 at BlazorTestApp2.Shared.Login.ValidateUser (System.String userName, System.String password) [0x0006d] in C:\Repo\BlazorTestApp2\BlazorTestApp2\Shared\Login.razor:83 at BlazorTestApp2.Shared.Login.LoginWithPassword () [0x0003f] in C:\Repo\BlazorTestApp2\BlazorTestApp2\Shared\Login.razor:116

Is this can be fixed anyway?

net version used : 3.1.0-preview4.19579.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
sukeshchandcommented, Mar 29, 2020

I have upgraded the version to 3.2.0-preview2.20160.5 and the issue got fixed.

For more info about new upgrade https://devblogs.microsoft.com/aspnet/blazor-webassembly-3-2-0-preview-2-release-now-available/

0reactions
WoodyJ007commented, May 29, 2020

I have the same problem using the latest builds. Internet has been telling me it’s a cors issue and I’ve been going around in circles. The above basic code works fine on console and other apps. Tried it on a blazor server side and works fine so something is up with WebAssembly? Tried it on two from scratch projects on two different PCs and same result. Any ideas? Known issue still?

FYI. it’s when connecting to a webapi on HTTP and project was set to configure for HTTPS.

` public async Task<string> GetSomeDataAsync() { var _client = new HttpClient();

    var response = await _client.GetStringAsync(@"http://dummy.restapiexample.com/api/v1/employees");

    return response;
}

`

Create Blazor server project with “Configure HTTPS” NOT ticked. Works Create Blazor server project with “Configure HTTPS” ticked. Works

Create the client web assembly project with “Configure HTTPS” NOT ticked. Works Create the client web assembly project with “Configure HTTPS” ticked. Fail

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve "TypeError: Failed to fetch" error on Blazor?
I am trying to send an HTTP request from my Blazor app to my ASP.NET Core API. I have breakpoints everywhere. The application...
Read more >
Blazor WebAssembly TypeError: Failed to fetch
So when I use that API endpoint from my Blazor App I received this exception. TypeError: Failed to fetch - at System.Net.Http.BrowserHttpHandler ...
Read more >
.NET 6 Blazor WASM: Downloading JSON string fails if ...
JavaScript.JSException: TypeError: Failed to fetch at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken ...
Read more >
[Fix]-How to resolve "TypeError: Failed to fetch" error on Blazor?
Unhandled exception rendering component: TypeError: Failed to fetch. WebAssembly.JSException: TypeError: Failed to fetch. I did a test to make request(s) ...
Read more >
How to resolve "TypeError: Failed to fetch" error on Blazor?
webassembly.jsexception: typeerror: failed to fetch. i did a test to make request(s) from my blazor webassembly app to action method with testing data, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found