Home > Enterprise >  Blazor | GetFromJsonAsyn error net::ERR_FAILED 200
Blazor | GetFromJsonAsyn error net::ERR_FAILED 200

Time:06-04

I'm tryting to to the following:

[Inject] public HttpClient HttpClient { get; set; } = default!;
public AzureActiveDirectoryUser[] AzureActiveDirectoryUsers { get; set; } = default!;

protected override async Task OnInitializedAsync()
    {
        var authenticationState = await authenticationStateTask;
        if (authenticationState.User?.Identity?.IsAuthenticated == true)
        {
            AzureActiveDirectoryUsers = await HttpClient.GetFromJsonAsync<AzureActiveDirectoryUser[]>(url); //this is line 52
        }
    }

But I'm getting the following error:

enter image description here

I don't really understand what it means.

What I'm trying to get is a JSON with usernames and emails (I have access to the URL and can verify that the data is there)

AzureActiveDirectoryUsers is just a class with an id and a mail.

(edit) I used to have this error. But it just suddenly stopped.

enter image description here

CodePudding user response:

In OnInitializedAsync not everything is initalized on the page.

Move the code in OnAfterRenderAsync and call StateHasChanged if needed.

CodePudding user response:

Run this very same code from Blazor Server App... If it does work, then the issue is a CORS one. If it's a CORS issue, then you can do nothing about it, unless the owner of the related service add you to the list of specific allowed clients or universally allow all clients to access the service, which so far he has failed to do. Note: The best solution to deal with such an issue (CORS issue) is to call a method residing on the Server side (it should be a Web API end point), and this method should perform the call to the service you want to query. Get the data from the service, and pass it to the calling method in your WebAssembly App.

Note that CORS is a "security" feature enforced by the Browser (JavaScript), but when you run the code from the server, no such limitation exists.

  • Related