0

Getting this error:

Access to fetch at 'https://myurl.azurewebsites.net/Player/1' from origin 'http://localhost:19006' has been blocked by CORS policy: 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.

I'm not the first with this error, but I feel like I have tried everything that one can find through searching for the problem. I'm developing a web API with ASP.net core, that's supposed to communicate with my react-native frontend. Problem is, I cannot for the life of me get the connection to work.

In program.cs I have added

var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
    builder.Services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  policy =>
                                  {
                                      policy.AllowAnyMethod();
                                      policy.AllowAnyHeader();
                                      policy.AllowAnyOrigin();
                                  });
            });

and

app.UseCors(MyAllowSpecificOrigins);

I have tried adding no cors to the method itself

[DisableCors]
[HttpGet("{id}")]
public List<Player> GetPlayers(int id)
{
    return (from c in _context.Player.Take(1)
            where c.PlayerId == id
            select c).ToList();
}

I even deployed the server and database on Azure (I was supposed to sooner or later anyway) just hoping that would allow me to get it to work. The API runs fine if I visit the URL and run it through that one. It also works great if I host it locally and go through the web.

On Azure I've changed my cors settings to allow everything: enter image description here

I can even access the API through expo web if I run it locally at the same time. But I need to be able to do it through my phone as well, or at least an android emulator. Neither of those works for neither a locally hosted server, or one that's on Azure.

How can I solve this?

linkedby
  • 148
  • 1
  • 8
  • Does it help ? [answer 01] (https://stackoverflow.com/questions/52896068/reactasp-net-core-no-access-control-allow-origin-header-is-present-on-the-r?rq=1) – joccafi Dec 15 '22 at 21:56
  • Unfortunately not, tried just about everything there. Setting the mode to 'no-cors' does remove the error, but then I fetch an empty body. – linkedby Dec 15 '22 at 22:17
  • Did you checked the browser logs and console? What error you are geeting there? If you specify the `URL` and methods does it still persisted? – Md Farid Uddin Kiron Dec 16 '22 at 09:27

1 Answers1

0

Actually, shortly after setting my Azure cors settings, it did indeed start to work. Finally, I can at least demo it. Unfortunately, I still have no solution that solves it when hosting locally.

linkedby
  • 148
  • 1
  • 8