vue---axiosmd5加密案例
程序员文章站
2022-08-13 18:52:29
vue---axios md5
1.npm install axios
2.安装md5:
npm install js-md5
3.在vue项目中得...
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); })
上一篇: 教你如何理解闭包