2016-12-02 51 views
0

我正在一个脚本来从我的API的一些数据:赶上()不处理404

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是不存在的页面,以便它返回一个404,但美中不足的是没有处理这个。为什么不?在我的控制台中,我有错误TypeError: Cannot read property 'data' of undefined。为什么它不运行failed()函数?我不明白。

+0

的可能的复制[承诺:再VS那么+抓(http://stackoverflow.com/questions/33278280/promise-then-vs-then-catch) – CMedina

+0

您使用的是什么版本的爱可信? – roger

+0

最新版本:0.15.2 – Jordy

回答

0

由错误信息判断,它看起来像“成功(response.data);”被称为。服务器是否有可能成功地返回了一个类似“Error 404”的页面,而不是实际返回http响应代码404?

+0

不,它返回一个404响应代码.. – Jordy

0

发现这是关系到一个自定义拦截器处理401错误(但不是404错误)...

+0

大概你的意思是“...一个自定义拦截器,它被设计来处理401错误,哪些*应该*重新抛出所有其他...但不是“? –

0

你可以impliment为404的支票。

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); 
    }); 

然后,你可能想要检查的是,以确保它是一个200返回。

axios.$http.get('/somedata') 
    .then(response => { 
    if(response.status === 200) 
     success(response.data); 
    else 
     failed(response) 
    }) 
    .catch((error) => { 
    failed(error); 
    });