Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

ASP.NET Core 2.2 CORS No 'Access-Control-Allow-Origin' header

$
0
0

I'm having trouble to consume a ASP.NET Core 2.2 web api.

This javascript code below works well, I can get all clients fine

fetch('https://10.20.0.20:8081/api/clients/list').then(data => { data.json().then(dt=>{console.log(dt)}) }).catch(error => { console.log(error)});

But this one doesn't work

fetch('https://10.20.0.20:8081/api/clients/list',{ "headers": {"content-type": "application/json"}}).then(data => { data.json().then(dt=>{console.log(dt)}) }).catch(error => { console.log(error)});

Well... It is what I have in my ConfigureServices method from Startup.cs

services.AddCors(setup => setup.AddPolicy("AllowAll", builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()));

My first line after Configure method from Startup.cs

app.UseCors("AllowAll");

<div>When I comment this line above both fetch stop working.</div> <div> </div> <div>I did this litle test just because I'm using angular to consume this API and I facing this same problem.</div> <div> </div><div>Error message:</div> <div>Access to fetch at 'https://10.20.0.20:8081/api/clients/Listar' from origin 'https://10.20.0.20:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.</div>

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>