Home > Blockchain >  Cannot execute PUT method on browser
Cannot execute PUT method on browser

Time:10-23

I'm traing to create an angular app

Well, when I call the API from Postman it work but when I try to call it using HTTP.put on a browser (firefox or chrome ) I always have this error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:5001/api/users/set-main-photo/25. (Reason: Did not find method in CORS header ‘Access-Control-Allow-Methods’).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:5001/api/users/set-main-photo/25. (Reason: CORS request did not succeed).

I make some research and I addid this to my header using the Interceptor Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD Access-Control-Allow-Origin: *

still same issue

any help please?

Note : HTTP.GET and HTTP.POST works fine Thank you

CodePudding user response:

You've run into a common issue when starting out with development. Due to security issues you are restricted from calls across domain names without it being explicitly configured to do so. Depending on your API you can allow any origin to call it.

Setting the headers on your request itself will not allow you to bypass this restriction you need to configure the API.

You can read https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS to get a better understanding.

Or as stated above this previous post covers the topic as well https://stackoverflow.com/a/10892392/9994146

CodePudding user response:

I resolve this using

  app.UseCors(x => x
                .AllowAnyMethod()
                .AllowAnyHeader()
                .SetIsOriginAllowed(origin => true) // allow any origin
                .AllowCredentials()); // allow credentials

before it was

 app.UseCors( x => x.AllowAnyHeader().AllowAnyHeader().WithOrigins("https://localhost:4200"));

Thank you

  • Related