代码之家  ›  专栏  ›  技术社区  ›  BT101

Axios HTTP调用始终被视为成功

  •  7
  • BT101  · 技术社区  · 8 年前

    因此,我正在尝试多种方法从axios HTTP调用中获取错误响应状态,但奇怪的事情正在发生。

            getData() {
                axios.get(`/api/article/getObserved.php`, axiosConfig)
                    .then(response => {
                        console.log('success');
                        console.log(response);
                    })
                    .catch(err => {
                        console.log('error');
                        console.log(err.status);
                        console.log(err.response.status)
                    });
            }
    

    所以我调用我的getObserved端点,尽管它正在返回 http_response_code(503); 它会 .then() 部分原因是it控制台日志“success”字符串。

    这是控制台的输出:

    获取 http://localhost/obiezaca/v2/api/article/getObserved.php 503(服务不可用)

    成功收藏夹文章。vue?31bd:83

    我打过几百个这样的电话 .catch 总是捕捉错误,即使我不会像其他语言那样抛出异常。但我也试过这样做:

            getData() {
                axios.get(`/api/article/getObserved.php`, axiosConfig)
                    .then(response => {
                        console.log('success');
                        console.log(response);
                    }, function (err) {
                        console.log('error');
                        console.log(err.status);
                        console.log(err.response.status);
                    })
                    .catch(err => {
                        console.log('error');
                        console.log(err.status);
                        console.log(err.response.status)
                    });
            }
    

    尽管我从端点返回了这个503错误请求,但它仍然不能处理“error”。为什么?


    我还想补充一点,我不认为 my endpoint 工作不正常,因为我用cURL和POSTMAN手动测试了它,一切都很好。


    编辑 由于当我不从端点获取数据时,响应是未定义的,并且我只需要处理一个错误(是否有数据),所以我只做了如下操作:

            getData() {
                axios.get(`/api/article/getObserved.php`, axiosConfig)
                    .then(response => {
                        if(response) {
                            this.articles = response.data.records;
                        } else {
                            this.noFavourite = true;
                            this.articles = [];
                        }
                    });
    

    它正在发挥作用。我会祈祷不要在打电话时遇到同样的问题,因为我需要处理几个不同的错误。

    1 回复  |  直到 8 年前
        1
  •  7
  •   BT101    8 年前

    此问题与我的httpInterceptor有关

    import axios from 'axios';
    import { store } from '../store/store';
    
    export default function execute() {
        axios.interceptors.request.use(function(config) {
            const token = store.state.token;
            if(token) {
                config.headers.Authorization = `Bearer ${token}`;
                //console.log(config);
                return config;
            } else {
                return config;
            }
        }, function(err) {
            return Promise.reject(err);
        });
        axios.interceptors.response.use((response) => {
            return response;
        }, (err) => {
            console.log(err.response.status)
            return Promise.reject(err); // i didn't have this line before
        });
    }
    

    它并没有在错误响应时返回承诺,所以在http调用承诺之后,它以某种方式将其视为成功。添加后 return Promise.reject(err); 在我的拦截器内工作正常