zoukankan      html  css  js  c++  java
  • vue项目对axios的全局配置

    一、axios.js文件中
    1.//axios 默认配置
    axios.defaults.timeout = 50000;   // 超时时间
    axios.defaults.baseURL = constant.OA_URL;    // 默认地址
    axios.defaults.responseType = 'json';    //类型动态设置
     
    2.//整理数据
    axios.defaults.transformRequest = function (data) {
    //data = Qs.stringify(data);
    data = JSON.stringify(data);
      return data;
    };

    3.// 路由请求拦截
    // http request 拦截器
    axios.interceptors.request.use(
      config => {
        //config.data = JSON.stringify(config.data);  
        config.headers[‘Content-Type‘] = ‘application/json;charset=UTF-8‘;
        //判断是否存在ticket,如果存在的话,则每个http header都加上ticket
        if (cookie.get("token")) {
            //用户每次操作,都将cookie设置成2小时
            cookie.set("token",cookie.get("token") ,1/12)    
            cookie.set("name",cookie.get("name") ,1/12)
         config.headers.token = cookie.get("token");
         config.headers.name= cookie.get("name");
        }
        
        return config;
      },
      error => {
        return Promise.reject(error.response);
      });

    4.// 路由响应拦截
    // http response 拦截器
    axios.interceptors.response.use(
      response => {
        if (response.data.resultCode=="404") {
            console.log("response.data.resultCode是404")
            // 返回 错误代码-1 清除ticket信息并跳转到登录页面
    //      cookie.del("ticket")
    //      window.location.href=‘http://login.com‘
            return
        }else{
            return response;
        }
      },
      error => {
        return Promise.reject(error.response)   // 返回接口返回的错误信息
      });
    export default axios;


    二、在mian.js文件中

    import axios from './axios/axios'
    Vue.prototype.axios = axios;


  • 相关阅读:
    iOS开发-文件管理
    MagicalRecord
    NSPredicate的
    Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
    iOS 开发者必不可少的 75 个工具
    UITableView 删除cell
    手势
    随机数
    PPT2016同时播放多个视频
    MATLAB运行时,弹出图片框影响电脑使用
  • 原文地址:https://www.cnblogs.com/abuya/p/10510548.html
Copyright © 2011-2022 走看看