error.response is undefined if axios request fails
up vote
0
down vote
favorite
I cannot access the error (response) status code if an axios request has failed in my Vue.js app. I cannot figure out why the response is undefined in both '.catch' and 'axios.interceptors.response'. I followed this instruction that demonstrates that 'error.response' can be easily accessed with a code like this:
axios.interceptors.response.use(
(response) => {
console.log(response);
return response;
},
(error) => {
handleApiFail(error.response);
});
If I add this code to 'main.js' in my app, 'handleApiFail' is called when a request fails, but error.response is undefined in the second lambda and the first lambda is not called. If a request succeeded the 'response' in the first lambda is defined and has the status code.
EDIT1: this is not an option because my OPTIONS requests do not require authorization. Also there are various posts describing the same situation.
axios
add a comment |
up vote
0
down vote
favorite
I cannot access the error (response) status code if an axios request has failed in my Vue.js app. I cannot figure out why the response is undefined in both '.catch' and 'axios.interceptors.response'. I followed this instruction that demonstrates that 'error.response' can be easily accessed with a code like this:
axios.interceptors.response.use(
(response) => {
console.log(response);
return response;
},
(error) => {
handleApiFail(error.response);
});
If I add this code to 'main.js' in my app, 'handleApiFail' is called when a request fails, but error.response is undefined in the second lambda and the first lambda is not called. If a request succeeded the 'response' in the first lambda is defined and has the status code.
EDIT1: this is not an option because my OPTIONS requests do not require authorization. Also there are various posts describing the same situation.
axios
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I cannot access the error (response) status code if an axios request has failed in my Vue.js app. I cannot figure out why the response is undefined in both '.catch' and 'axios.interceptors.response'. I followed this instruction that demonstrates that 'error.response' can be easily accessed with a code like this:
axios.interceptors.response.use(
(response) => {
console.log(response);
return response;
},
(error) => {
handleApiFail(error.response);
});
If I add this code to 'main.js' in my app, 'handleApiFail' is called when a request fails, but error.response is undefined in the second lambda and the first lambda is not called. If a request succeeded the 'response' in the first lambda is defined and has the status code.
EDIT1: this is not an option because my OPTIONS requests do not require authorization. Also there are various posts describing the same situation.
axios
I cannot access the error (response) status code if an axios request has failed in my Vue.js app. I cannot figure out why the response is undefined in both '.catch' and 'axios.interceptors.response'. I followed this instruction that demonstrates that 'error.response' can be easily accessed with a code like this:
axios.interceptors.response.use(
(response) => {
console.log(response);
return response;
},
(error) => {
handleApiFail(error.response);
});
If I add this code to 'main.js' in my app, 'handleApiFail' is called when a request fails, but error.response is undefined in the second lambda and the first lambda is not called. If a request succeeded the 'response' in the first lambda is defined and has the status code.
EDIT1: this is not an option because my OPTIONS requests do not require authorization. Also there are various posts describing the same situation.
axios
axios
edited Nov 13 at 17:09
asked Nov 13 at 15:07
Alexey Starinsky
49010
49010
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
This is an idiosyncrasy of axios. A quick solution to this is to serialize the response:
JSON.stringify(error)
Please refer to this GitHub issue for more info: https://github.com/axios/axios/issues/960
As someone pointed out there, you can check the error status code in the action and run some other commit depending on it.
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
1
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
1
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
add a comment |
up vote
0
down vote
accepted
The lack of
access-control-allow-origin: *
header in the response caused the browser to block my request.
Adding the header makes axios work fine.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This is an idiosyncrasy of axios. A quick solution to this is to serialize the response:
JSON.stringify(error)
Please refer to this GitHub issue for more info: https://github.com/axios/axios/issues/960
As someone pointed out there, you can check the error status code in the action and run some other commit depending on it.
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
1
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
1
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
add a comment |
up vote
0
down vote
This is an idiosyncrasy of axios. A quick solution to this is to serialize the response:
JSON.stringify(error)
Please refer to this GitHub issue for more info: https://github.com/axios/axios/issues/960
As someone pointed out there, you can check the error status code in the action and run some other commit depending on it.
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
1
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
1
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
add a comment |
up vote
0
down vote
up vote
0
down vote
This is an idiosyncrasy of axios. A quick solution to this is to serialize the response:
JSON.stringify(error)
Please refer to this GitHub issue for more info: https://github.com/axios/axios/issues/960
As someone pointed out there, you can check the error status code in the action and run some other commit depending on it.
This is an idiosyncrasy of axios. A quick solution to this is to serialize the response:
JSON.stringify(error)
Please refer to this GitHub issue for more info: https://github.com/axios/axios/issues/960
As someone pointed out there, you can check the error status code in the action and run some other commit depending on it.
answered Nov 13 at 15:25
Simone
11.6k95489
11.6k95489
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
1
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
1
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
add a comment |
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
1
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
1
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean adding 'error = JSON.parse(JSON.stringify(error))' into the second lambda?
– Alexey Starinsky
Nov 13 at 15:35
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
Did you mean Vuex action? How does it relate to axios?
– Alexey Starinsky
Nov 13 at 15:40
1
1
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
Check the discussion on GitHub, looks like either axios or vuex (or maybe both) are mangling the error response. The trick (as you pointed out as well) is to JSON.stringify and then JSON.parse the error. Just try that and you should be able to see the actual error response
– Simone
Nov 14 at 9:57
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
I tried 'error = JSON.parse(JSON.stringify(error))' in the interceptor, but with no success.
– Alexey Starinsky
Nov 14 at 11:10
1
1
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
I'm afraid I can't help much further as I'm not familiar with Vuex, but you might be able to find the solution on the GitHub discussion. They labeled it as a non-issue and closed it, but people are still commenting there.
– Simone
Nov 14 at 11:35
add a comment |
up vote
0
down vote
accepted
The lack of
access-control-allow-origin: *
header in the response caused the browser to block my request.
Adding the header makes axios work fine.
add a comment |
up vote
0
down vote
accepted
The lack of
access-control-allow-origin: *
header in the response caused the browser to block my request.
Adding the header makes axios work fine.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The lack of
access-control-allow-origin: *
header in the response caused the browser to block my request.
Adding the header makes axios work fine.
The lack of
access-control-allow-origin: *
header in the response caused the browser to block my request.
Adding the header makes axios work fine.
answered Nov 27 at 14:47
Alexey Starinsky
49010
49010
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283932%2ferror-response-is-undefined-if-axios-request-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown