+
80
-

vue中如何使用axios进行ajax拦截数据?

vue
vue中如何使用axios进行ajax拦截数据?

网友回复

+
0
-

axios设置一下就好了

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BFW NEW PAGE</title>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/axios-0.18.js"></script>
    <script type="text/javascript" charset="utf-8">
        const axiosInstance = axios.create({
            timeout: 2000,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            }
        })
        function interceptAjax(res) {
            alert("拦截到了");

            return {};

            const tmp = res.headers['exceptionaction'];

            if (!tmp) return res;
            //系统已经登陆
            const token = RxUtil.getCache('token');
            if (!token) {
                router.push({
                    name: 'login'
                });
                return;
            }
        }
        axiosInstance.interceptors.response.use(res => interceptAjax(res));
        axiosInstance.get('/').
        then(response => {
            console.log(response);

        }).
        catch(e => {
            console.log(e);
        });

    </script>
    <style>
    </style>
</head>
<body>

</body>
</html>

我知道答案,我要回答