-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Axios vs jQuery post CORS issue #113
Comments
What is the error that you are getting? |
is jQuery sending something that Axios isn't? Headers or preflight ? |
What browser are you using when you get this error? |
Chrome 45 |
Will you try making this request from your app, and let me know the result? axios.get('https://api.github.com/users/benjamingeorge')
.then(function (res) {
console.log(res.data);
})
.catch(function(res) {
if(res instanceof Error) {
console.log(res.message);
} else {
console.log(res.data);
}
}); |
Sorry for the delay. I get an object back with all the data. Again, it's not get methods that are the problem, it's the POST method that has the issue. |
+1 |
1 similar comment
+1 |
Stumbled upon this very same issue. jQuery AJAX POST works, Axios (and isomorphic-fetch FWIW) POSTs fail in the preflight phase. What's even more weird is if I "Copy as cURL" both of the requests from Chrome inspector's network tab, they're identical! |
I had this problem as well, but I got it working now as I simply had Access-Control-Allow-Origin set up wrong on the server side. I suspect that the rest of you have this as well because jquery POST sends the requests as application/x-www-form-urlencoded as default, and thus doesn`t get preflighted. While axios correctly sends it as application/json and will be preflighted. |
👍 |
+1 |
Homyk -> What CORS setting did you change on the server side to properly accept application/json in preflight with AXIOS? Can you explain? For example, I am using Tomcat, and sending post requests "with credentials" and Axios is not working for us. |
peterpuzos -> I`m using embedded Jetty, but an equivalent will exist for tomcat. FilterHolder filter = new FilterHolder(); allowedOrigins setting should have a specific IP in prod. |
Thanks for the advice. We actually managed to get it working after doing some reconfiguration (removed CXF from the equation). We can confirm that it doesn't seem to be an AXIOS issue at all! Cheers! |
+1 |
5 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
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. |
@leyume |
@steelx were you able to find a solution? I have the same issue, OPTIONS is successful. but POST throws an error. EDIT: nevermind, was an issue with DB connection |
Spent almost a day testing POST request via Axios with unsuccessful result. The Jquery worked just out of the box. After trying out all suggested setting none worked and frustrating. Later I compared the data being sent by Axios POST and jquery POST and the source format in Chrome Dev Tool showed; jquery sent data in the url string fromat i.e., |
Hi @dhirajbasukala I'm having the same issue. Can you post an example please? |
+1 issue |
@unuigbee, below is example that worked for me. where as earlier JSON object being sent case didnt' work which was as below: I hope it helps. |
@dhirajbasukala Thanks I tried it but it didn't work. I had to set Headers for Access-Control-Allow-Origin to all (*) on my dev server. I know it's not safe but I had to for testing purposes. |
+1 |
maybe you need check server accepts content type, I used angular $http from jquery.ajax encounter this problem.
jquery v1 default content-type is 'application/x-www-form-urlencoded;charset=utf-8' |
I'm closing this issue as it's the expected behaviour. We're considering adding a new parameter to automatically configure the |
@rubennorte Is this automatic configuration implemented? |
@mzabriskie Is the above automatic configuration idea about to be implemented? It appears that this makes it difficult to use axios, for certain use cases. Thanks in advance! |
I tried param like "a=3&b=4", it works |
+1 |
as @leyume said, if it worked with jquery, than passing config object with headers fixed the problem for me. Also make sure to have "Access-Control-Allow-Origin" = "*" set on the server side. |
I've spent all night trying to make axios work. It always sends OPTIONS instead of POST and my CORS cofiguration gets ignored. Vue Resource works for me. Vue.js 2 (front), Laravel 5.4 (api), Ubuntu (dev machine), Google Chrome & Firefox |
HTTPS cors HTTP? Hello, how do I communicate using React axios. Accurate |
+1 |
1 similar comment
+1 |
you can must use qs to serialize your data |
+1 |
2 similar comments
+1 |
+1 |
@Ethan0007 Swap your header to: headers: { "Content-Type": "application/x-www-form-urlencoded" }, Otherwise the server/api will need to accept application/json. See the example above for the backend fix in jetty: |
When doing a CORS post request, jQuery succeeds but Axios gets a CORS error in the console.
This gets a CORS error
But this works
Am I missing something with Axios? My server is already configured for CORS requests.
The text was updated successfully, but these errors were encountered: