admin管理员组文章数量:1431727
I'm making a script to fetch some data from my api:
const success = (response) => {
console.log(response);
};
const failed = (error) => {
console.log(error);
};
axios.$http.get('/somedata')
.then((response) => {
success(response.data);
})
.catch((error) => {
failed(error);
});
/somepage
is a non-existing page so it returns a 404. But the catch is not handling this. Why not? In my console I have the error TypeError: Cannot read property 'data' of undefined
. Why does it not run the failed()
function? I don't understand.
I'm making a script to fetch some data from my api:
const success = (response) => {
console.log(response);
};
const failed = (error) => {
console.log(error);
};
axios.$http.get('/somedata')
.then((response) => {
success(response.data);
})
.catch((error) => {
failed(error);
});
/somepage
is a non-existing page so it returns a 404. But the catch is not handling this. Why not? In my console I have the error TypeError: Cannot read property 'data' of undefined
. Why does it not run the failed()
function? I don't understand.
- Possible duplicate of Promise : then vs then + catch – CMedina Commented Dec 2, 2016 at 22:58
- what version of axios are you using? – roger Commented Dec 2, 2016 at 23:00
- Latest version: 0.15.2 – Jordy Commented Dec 2, 2016 at 23:01
- seems this has potentially been an issue in the past, take a look at this thread: github./mzabriskie/axios/issues/41 and maybe check out what your 'validateStatus' config is set to (information on that can be found in the README: github./mzabriskie/axios/blob/master/README.md) – roger Commented Dec 2, 2016 at 23:12
- Thank you, see my own answer. You pointed me in the right direction ;) – Jordy Commented Dec 2, 2016 at 23:21
3 Answers
Reset to default 1Found out it was related to a custom interceptor handling 401-errors (but not 404 errors)...
Judging by the error message, it looks like "success(response.data);" is being called. Is it possible the server is successfully returning a page that says something like "Error 404" rather than actually returning http response code 404?
You could impliment a check for 404s.
axios.$http.get('/somedata')
.then(response => {
if(response.status !== 404) //or any status code really
success(response.data);
else
failed(response)
})
.catch((error) => {
failed(error);
});
Then again what you probably want to check for is to make sure it's a 200 that returns.
axios.$http.get('/somedata')
.then(response => {
if(response.status === 200)
success(response.data);
else
failed(response)
})
.catch((error) => {
failed(error);
});
本文标签: javascriptCatch() not handling 404Stack Overflow
版权声明:本文标题:javascript - Catch() not handling 404 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745549784a2662885.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论