2016-12-02 50 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); 
    });