赞
踩
request.interceptors.request.use(
config => {
/**
* 处理请求头
* 1.token 2.防刷短信
*/
if(token){ config.headers.Authorization= JSON.parse(token) }
//防刷短信(pdd)
config.headers.AcceptParam = 'application/no-referrer-urlencoded';
return config
}, function (error) {
return Promise.reject(error)
}
)
注意:post请求时需要QS数据格式化,否则报错
import QS from 'qs'; request.interceptors.request.use( config => { if (config.method == 'post') { config.data = { ...config.data, token: '1234' } // 需要将数据格式化,否则传到后台就错了 config.data = QS.stringify(config.data) }else if (config.method == 'get') { config.params = { token: '1234', ...config.params } } return config }, function (error) { return Promise.reject(error) } )
import QS from 'qs';
request.interceptors.request.use(
config => {
if(config.method == 'post'){
let defaultParams = {token: '123'};
// 默认值与接口传来的参数进行合并(注:接口参数与默认值不可重复)
config.data = Object.assign(defaultParams, config.data);
config.data = QS.stringify(config.data)
}
return config
}, function (error) {
return Promise.reject(error)
}
)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。