question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios vs jQuery post CORS issue

See original GitHub issue

When doing a CORS post request, jQuery succeeds but Axios gets a CORS error in the console.

This gets a CORS error

Axios.post(endpoint, { email_address : emailAddress})
      .then(function (response) {
        isSentSuccessful = true;
      })
      .catch(function (response) { 
        errorAnimation();
      });

But this works

    jQuery.post(endpoint, { email_address : emailAddress },
        function(resp){
         isSentSuccessful = true;
        })
        .fail(function(resp){
          errorAnimation();
        });

Am I missing something with Axios? My server is already configured for CORS requests.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:19
  • Comments:48 (4 by maintainers)

github_iconTop GitHub Comments

23reactions
leyumecommented, Apr 28, 2016

Another way to deal with this, especially if it worked in jQuery

var config = {
       headers: {
             'Content-Type': 'application/x-www-form-urlencoded'
       },
       params: {
             email_address: emailAddress
       }
};
axios.post( endpoint, {}, config)
.then(function (response) {
       console.log(response);
})
.catch(function (response) {
       console.log(response);
});

From Homyk comment on Dec 9, 2015, was able to come up with this and it worked.

5reactions
rafaeleyngcommented, Dec 23, 2015

I was setting up a little API to learn Grails. Doing a POST request, only by changing from jQuery to Axios, I went from getting this:

screen shot 2015-12-23 at 3 36 45 pm

to this:

screen shot 2015-12-23 at 3 35 43 pm

Read more comments on GitHub >

github_iconTop Results From Across the Web

jQuery ajax working but axios giving CORS error
In your jQuery, you are manually encoding the data as JSON and failing to set the correct Content-Type header. As a result you...
Read more >
Axios vs. fetch(): Which is best for making HTTP requests?
Axios is not always an ideal solution; depending on your needs, there are sometimes better options for making HTTP requests.
Read more >
ReactJS CORS Options - GeeksforGeeks
In axios: Axios always use base URL to start the request and the browser confirms that in the beginning HTTP OPTIONS requests by...
Read more >
[Solved]-React CORS issue with fetch for POST request-Reactjs
Coding example for the question React CORS issue with fetch for POST ... Getting Bad request for POST request using Axios and CORS...
Read more >
Fixing Common Problems with CORS and JavaScript
Many websites have JavaScript functions that make network requests to a server, such as a REST API. The web pages and APIs are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found