zoukankan      html  css  js  c++  java
  • axios

    export default {
      get: function (url, params, response) {
        return apiAxios('GET', url, params, response)
      },
      post: function (url, params, response) {
        url = process.env.NODE_ENV != 'production' ? 'pc' + url : url;
        return postOri(url, params, {},response)
      },
      postBlob: function (url, params, response) {
        return postBlob('POST', url, params, response)
      },
      put: function (url, params, response) {
        return apiAxios('PUT', url, params, response)
      },
      delete: function (url, params, response) {
        return apiAxios('DELETE', url, params, response)
      },
      posth5: function (url, params, response) {
        return h5Axios('POST', url, params, response)
      },
      getdat: function (url, params, response) {
        return datAxios('GET', url, params, response)
      },
      postdat: function (url, params, response) {
        return datAxios('POST', url, params, response)
      },
      env:process.env.NODE_ENV != 'production' ? 'pc': '',
    }
    
    
    
    import axios from 'axios'
    import qs from 'qs'
    import {
      Message
    } from 'element-ui';
    import Vue from 'vue'
    import router from '../router'
    axios.interceptors.request.use(
      config => {
          config.headers.lan = (localStorage.getItem('lanCode') || 'cn');
          if(config.method=='post'){
            config.transformRequest = [function (data,type) {
              data = qs.stringify(data)
              return data;}
            ];
          }
          console.log(config,'config')
        return config;
      },
      err => {
        return Promise.reject(err);
    });
    let http = axios.create({
      withCredentials: true,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        'lan': localStorage.getItem('lanCode') || 'cn'
      },
      transformRequest: [function (data,type) {
        data = qs.stringify(data)
        return data;
      }]
    });
    let datHttp = axios.create({
      withCredentials: true,
      headers: {
        'Content-Type': 'application/json;charset=utf-8',
        'lan': localStorage.getItem('lanCode') || 'cn'
      },
      transformRequest: [function (data,type) {
        return data;
      }]
    });
    function postOri(url, data = {}, config = {
      "headers": {
       'Content-Type': 'application/json;charset=utf-8',
       'lan': localStorage.getItem('lanCode') || 'cn'
      },
     },response) {
      return new Promise((resolve, reject) => {
       axios.post(url, data, config)
       .then(function (res) {
        let data = res.data;
        if (data.code == 1001) {
          Message.warning(data.message);
          localStorage.clear();
          sessionStorage.clear();
          // Router.push({path:'/login'});
          setTimeout(_ => {
            router.replace({
              path: '/login' //自己修改
            })
          }, 100);
          return;
        }
        if (res.status == 200) {
          response(data);
        }
         
      }).catch(function (err) {
        if (err.response && err.response.status == 500) {
          Message.warning('系统异常');
          response(err);
        }
      })
      })
     }
    axios.interceptors.request 请求之前进来拦截
    import axios from 'axios'
    import qs from 'qs'
    import {
    Message
    } from 'element-ui';
    import Vue from 'vue'
    import router from '../router'
    axios.interceptors.request.use(
    config => {
    config.headers.lan = (localStorage.getItem('lanCode') || 'cn');
    if(config.method=='post'){
    config.transformRequest = [function (data,type) {
    data = qs.stringify(data)
    return data;}
    ];
    }
    console.log(config,'config')
    return config;
    },
    err => {
    return Promise.reject(err);
    });
    let http = axios.create({
    withCredentials: true,
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    'lan': localStorage.getItem('lanCode') || 'cn'
    },
    transformRequest: [function (data,type) {
    data = qs.stringify(data)
    return data;
    }]
    });
  • 相关阅读:
    make学习笔记(一)
    进程学习笔记(二)进程间通信
    背包入门(01背包,完全背包,多重背包)
    线程学习笔记(一)
    设计模式学习笔记
    Python学习笔记(一)
    poj1276(多重背包)裸题
    【树莓派】树莓派小车(二)树莓派、小车和红外线模块连接(多图)
    【Java Web】IDEA如何创建及配置Web项目(多图)
    【树莓派】树莓派小车(一)小车配件选购及安装(多图)
  • 原文地址:https://www.cnblogs.com/guidan/p/10489277.html
Copyright © 2011-2022 走看看