1

我正在制作一个脚本来从我的 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是一个不存在的页面,所以它返回一个 404。但问题没有处理这个问题。为什么不?在我的控制台中,我有错误TypeError: Cannot read property 'data' of undefined。为什么它不运行该failed()功能?我不明白。

4

3 回答 3

1

发现它与处理 401 错误(但不是 404 错误)的自定义拦截器有关...

于 2016-12-02T23:21:10.783 回答
0

您可以对 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);
  });
于 2016-12-04T01:53:50.240 回答
0

从错误信息来看,它看起来像“success(response.data);” 正在被调用。服务器是否有可能成功返回一个显示“错误 404”之类的页面,而不是实际返回 http 响应代码 404?

于 2016-12-02T23:05:24.287 回答