vue---axios md5加密

1.安装axios:

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);
})

 

你可能感兴趣的:(VUE)