I have a react app, in which I am trying to make an API call using fetch() method.
fetch("https://example.com/user");
something like this. but it is throwing an error like below.
from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3000' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
my project is running on localhost:3000
and calling API like https://example.com/user
.
I kept Access-Control-Allow-Origin: *
fetch("https://example.com/user", {
headers: {
"Access-Control-Allow-Origin": "*"
})
but got below error.
Access to fetch at 'https://example.com/user` from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
tried mode:no-cors
fetch("https://example.com/user", {
headers: {
"mode":"no-cors"
})
but got same error i.e.
Access to fetch at 'https://example.com/user` from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
for both options
fetch("https://example.com/user", {
headers: {
"Access-Control-Allow-Origin": "*"
"mode":"no-cors"
})
got the below error.
Access to fetch at 'https://example.com/user`' from origin 'http://localhost:3000' 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.