I'm trying to fetch from an external API in ReactJS.
This is the function I'm using to fetch:
fetch(
`https://www.iban-test.de/api/check/${input}?authcode=${IBAN_TEST_API}`
)
.then(data => setIbanResponse(data))
.catch(error => setIbanResponse(error));
}
When I console.log(ibanResponse)
(I'm using Hooks
) I get an
Access to fetch at 'https://www.iban-test.de/api/check/XXXX?authcode=XXXX' 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.
TypeError: Failed to fetch
index.js:30 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://www.iban-test.de/api/check/XXXX?authcode=XXXX with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
In the network inspector, I can see a 200
response from the API, and if I make the request from my browser, it works just fine.
How can I fix this fetch request?
I've already tried this:
fetch(
`https://www.iban-test.de/api/check/${input}?authcode=${IBAN_TEST_API}`,
{
headers: {
"Access-Control-Allow-Origin": "*"
}
}
)
.then(data => setIbanResponse(data))
.catch(error => setIbanResponse(error));
}
And this:
fetch(
`https://www.iban-test.de/api/check/${input}?authcode=${IBAN_TEST_API}`
)
.then(response => response.json())
.then(data => setIbanResponse(data))
.catch(error => setIbanResponse(error));
}
But it returns the same error.
Edit: I'm editing the question to anyone searching for this same problem. I implemented the solution @Matin Sasan and @sideshowbarker said and it worked like a charm. I had to use a proxy to make the request.