Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable CORS from front-end in React with axios?

I am using React on the front-end and I'm calling API from another domain which I don't own. My axios request:

axios(requestURL, {
        method: 'GET',
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json',
            'Authorization': key,
            withCredentials: true,
            mode: 'no-cors',
          }

I keep on getting the same error: CORS header ‘Access-Control-Allow-Origin’ missing. Is this something I can overcome from the frontend? I know for a fact that people use that API so it can't be backend fault, right? I tried requesting a lot of APIs and not even one worked with my code. I tried using https://cors-anywhere.herokuapp.com and it worked fine for like a week, I think its down today. I want my site to stay 24/7 so using a proxy is not an option

like image 769
IdleSolution Avatar asked Mar 16 '19 19:03

IdleSolution


1 Answers

One can use CORS-anywhere. It is a NodeJS reverse proxy which adds CORS headers to the proxied request.

If I want to add CORS to https://test-example.com, then I'd just do it as follows:

https://cors-anywhere-herokuapp.com/https://test-example.com
like image 111
Yashwin Munsadwala Avatar answered Sep 20 '22 14:09

Yashwin Munsadwala