Skip to content
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

Can't catch CORS ERROR #838

Closed
ray0324 opened this issue Apr 12, 2017 · 12 comments
Closed

Can't catch CORS ERROR #838

ray0324 opened this issue Apr 12, 2017 · 12 comments

Comments

@ray0324
Copy link

ray0324 commented Apr 12, 2017

Can't get access to axios.interceptors.response whith cors err No 'Access-Control-Allow-Origin' header is present。

@jcready
Copy link
Contributor

jcready commented Apr 18, 2017

Browsers make the entire response opaque when preflight responses fail, see /lib/xhr.js line 85.

@lrettig
Copy link

lrettig commented May 25, 2017

I'm running into this too. What's best practice around handling this sort of situation? Is there any way to get more info on what went wrong so we can interpret the error and report it to the user or attempt to intercept the error?

@jcready
Copy link
Contributor

jcready commented May 25, 2017

The best you're going to do is something like this:

axios.interceptors.response.use((response) => response, (error) => {
  if (typeof error.response === 'undefined') {
    alert('A network error occurred. '
        + 'This could be a CORS issue or a dropped internet connection. '
        + 'It is not possible for us to know.')
  }
  return Promise.reject(error)
})

@lrettig
Copy link

lrettig commented May 25, 2017 via email

@jcready
Copy link
Contributor

jcready commented May 25, 2017

Again, this isn't an issue with axios. Browsers will not allow JS to see any response information when there is a CORS issue for security reasons. You can test this yourself with the following:

x = new XMLHttpRequest()
x.onerror = (e) => console.log('THIS IS ALL THE INFO WE HAVE', e, x)
x.open('GET', 'https://www.google.com')
x.send()

You'll see that there is no helpful information. There is no way to distinguish between no internet connection, bad DNS, or CORS issues, etc. All you know is that there was a Network Error.

@lrettig
Copy link

lrettig commented May 25, 2017 via email

@gcollombet
Copy link

The issue is also happening when their is a preflight request OPTIONS that is validated by the server (so no CORS error happen, the request is valid, and then a regular request return a 400 error).
OPTIONS 200 OK
POST 400 Bad Resquest

@IAMtheIAM
Copy link

IAMtheIAM commented Jul 24, 2018

I spent hours trying to figure out why I was getting a 400 response. I tried angular's HttpClient, Axios, and jQuery ajax. Finally I realized it was a CORS issue. Lol there goes a few hours.

It wasn't saying anything at first. just 400 Bad Request. Then I checked the console and saw the browser error message Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. I was over in the network tab and it really doesn't give any useful information at all from there, where you would expect it to be.

@pxwee5
Copy link

pxwee5 commented Dec 4, 2019

#838 (comment)

@jcready Can this not be implemented into Axios core as default?

@inspire22
Copy link

This old, but IMO rendering a console message is the way to handle this, it should create an error with e.message set to the cors error, rather than the generic "Error: Network Error" that is the default.

@jcready
Copy link
Contributor

jcready commented Apr 27, 2020

@inspire22 if you read through the old comments on this thread you'll see that axios has no way to know it is anything more specific than a network error.

@inspire22
Copy link

Oops, my bad. Definitely not a good design choice / bug by chrome, so we get confused people like me here :) Thanks for being a part of this nice project!

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants