aspnetcore: Error when trying get data from real server
Hello,
I have issue with this code from demo:
@page "/fetchdata"
@inject HttpClient Http
@if (door == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class='table'>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Version</th>
</tr>
</thead>
<tbody>
<tr>
<td>@door.id</td>
<td>@door.name</td>
<td>@door.version</td>
</tr>
</tbody>
</table>
}
@functions {
Door door;
protected override async Task OnInitAsync()
{
door = await Http.GetJsonAsync<Door>("http://localhost:58009/api/locations/1/doors/2");
}
class Door
{
public int id { get; set; }
public int? locationId { get; set; }
public string name { get; set; }
public int strikeTime { get; set; }
public int extendedAccessTime { get; set; }
public int heldOpenTime { get; set; }
public string version { get; set; }
}
}
I’m getting error in browser console window:
SEC7120: Origin http://localhost:5375 not found in Access-Control-Allow-Origin header.
WASM: [System.Net.Http.HttpRequestException] TypeError: Failed to fetch
4 WASM: — End of stack trace from previous location where exception was thrown —
If I just simply request if in browser => “http://localhost:58009/api/locations/1/doors/2”, I get this json response: { “id”:2, “locationId”:1, “name”:“216office”, “strikeTime”:3, “extendedAccessTime”:6, “heldOpenTime”:10, “version”:“AAAAAAAAKMw=” }
It is possible that need setup header content type to “application/json”?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (5 by maintainers)
@Gyciauskas To enable CORS you need to update the server project (not the client Blazor project). See https://docs.microsoft.com/en-us/aspnet/core/security/cors for details on how to enable CORS in ASP.NET Core.
You need
Microsoft.AspNetCore.Corshttps://www.nuget.org/packages/Microsoft.AspNetCore.Cors/