当前位置: 移动技术网 > IT编程>网页制作>CSS > vue---axiosmd5加密案例

vue---axiosmd5加密案例

2019年01月14日  | 移动技术网IT编程  | 我要评论

    vue---axios md5

    1.npm install axios

    2.安装md5:

    npm install js-md5

    3.在vue项目中得main.js中全局引入:

    import axios from 'axios';
    import md5 from 'js-md5';

    4.在main.js中加入以下代码:

    const http = axios.create({
      timeout: 1000 * 30,
      withcredentials: true,
      headers: {
        'content-type': 'application/json; charset=utf-8'
      }
    })
    http.interceptors.request.use(config => {
      // 请求头带上token
      let time = new date().gettime();
      config.params['time'] = time;
      config.headers['sign'] = md5('与后台对应的字符串'+time);
      return config
    }, error => {
      return promise.reject(error)
    })
    
    vue.prototype.$axios= http;

    5.在中运用axios即可:

    this.$axios({
         method: 'post',
         url:'项目的请求地址',
         params: params
    }).then((res)=>{
        //成功的回调
        console.log(res);
    },function(error){
        //失败的回调
        console.log(error);
    })

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网