vue配置请求头,设置请求拦截
import axios from "axios";import qs from "querystring";axios.defaults.timeout = 60000;axios.defaults.baseURL = window.apiHost;//window.apiHost可以在main.js里进行设置axios.defaults.withCredentials = false;axio
·
import axios from "axios";
import qs from "querystring";
axios.defaults.timeout = 60000;
axios.defaults.baseURL = window.apiHost; //window.apiHost可以在main.js里进行设置
axios.defaults.withCredentials = false;
axios.interceptors.request.use(
config => {
if (config.method === "POST") {
config.data = qs.stringify(config.data, { arrayFormat: "indices" });
}
//当使用Ie浏览器的时候,可能会出现数据更新了,但是接口请求数据不刷新的情况,加上下面这段代码即可
if (config.method === 'get' || config.method === 'GET') {
config.params = {
_t: Date.parse(new Date()) / 1000,
...config.params
}
}
if (!config.headers["Content-Type"]) {
config.headers["Content-Type"] = "application/json;utf-8";
}
if (localStorage.getItem("userInfo")){
var userinfo=localStorage.getItem("userInfo");
config.headers["Authorization"]="Bearer "+JSON.parse(userinfo).Token
}
return config;
},
error => {
return Promise.reject(error);
}
);
export default axios;
更多推荐
已为社区贡献4条内容
所有评论(0)