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

Error response with application/json body throws InvalidStateError #171

Closed
aukevanleeuwen opened this issue Dec 14, 2015 · 8 comments
Closed

Comments

@aukevanleeuwen
Copy link

When I return a validation error message from my server as with content type application/json with status code 400 this throws the following error:

Uncaught InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'json').

on this line: https://github.com/mzabriskie/axios/blob/f9c46c5c1da731c150febd347708f229e9bc74ab/lib/adapters/xhr.js#L75. Apparently it's not allowed to access the responseText property if the responseType does not match '' or 'text'.

You seem to be aware of this since you are checking exactly this a few lines earlier (https://github.com/mzabriskie/axios/blob/f9c46c5c1da731c150febd347708f229e9bc74ab/lib/adapters/xhr.js#L62).

I'd submit a pull request, but I'm not entirely sure what case is meant by the (request.responseText && xDomain) code...

@mzabriskie
Copy link
Member

I think this could be resolved by switching the order of the conditional check:

(xDomain && request.responseText)

This way we only reference request.responseText if it's valid.

aukevanleeuwen pushed a commit to aukevanleeuwen/axios that referenced this issue Dec 14, 2015
Don't read responseText unless the responseType is either 'text', '' or
undefined.
aukevanleeuwen pushed a commit to aukevanleeuwen/axios that referenced this issue Dec 14, 2015
Don't read responseText unless the responseType is either 'text', '' or
undefined/null.
@aukevanleeuwen
Copy link
Author

Sorry I was doing something else, but had this fix commited on my fork already before I saw your comment. Not sure what you prefer, but you seemed to have the check already in place so I just thought I'd use it.

@mzabriskie
Copy link
Member

I think it would be safer to do as I suggested above. Thanks for the PR!

aukevanleeuwen pushed a commit to aukevanleeuwen/axios that referenced this issue Dec 14, 2015
mzabriskie added a commit that referenced this issue Dec 14, 2015
Fixing #171, don't fail on error responses with json payloads
@tomoya
Copy link

tomoya commented Dec 16, 2015

A few minutes ago, I encountered same issue. but, is was already fixed. very nice! 👍

@solepano
Copy link

solepano commented Jun 30, 2016

Hi, I'm using axios within a react + redux app and I'm getting same error when a request that expects a JSON result fails with a 400 error:

DOMException: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'json').

Is the issue re-opened?

@GerryFudd
Copy link

GerryFudd commented Jan 5, 2017

I just encountered the same problem.

(Edit: I redact my request to reopen this. I was seeing an error from another package that was reported in such a way that it looked like there was an issue with the way axios was handling api responses.)

@ihorml
Copy link

ihorml commented Apr 9, 2017

If you use redux-axios-middleware just take a look at this issue

@qtttttttttting
Copy link

I encounter similar problem @0.16.2.
ajaxInject.js:1 Uncaught DOMException: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'blob').
Thank you for your help~

@axios axios locked and limited conversation to collaborators May 21, 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

7 participants