Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios POST request is complaining about cross origin, but curl request works fine

Tags:

reactjs

axios

 let url = 'http://localhost:4001/api/v1/sessions/new';
      axios.post(url, data)
        .then(function(resp) {
          console.log('resp returned is ' + JSON.stringify(resp));
        });

I see this error in chrome console"

Failed to load http://localhost:4001/api/v1/sessions/new: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4044' is therefore not allowed access. The response had HTTP status code 405.

If I just try and curl the exact same API call it works fine:

curl -d "[email protected]&password=hello123" -X POST http://localhost:4001/api/v1/sessions/new

{"apiResponse":{"success":true,"errors":[]}}

Why does it work with CURL but not with axios?

like image 566
Blankman Avatar asked Oct 20 '25 21:10

Blankman


1 Answers

Anything that isn't running in a browser doesn't care about CORS. If you'd like your web app to make requests to your server add the Access-Control-Allow-Origin header to your server's response with the most permissive value being *:

Access-Control-Allow-Origin: *
like image 148
Andy Gaskell Avatar answered Oct 23 '25 10:10

Andy Gaskell