application/x-www-form-urlencoded方式对post请求传参
问题
vue项目中,axios用application/x-www-form-urlencoded方式对post请求传参,但是按正常的data方式传一直获取不到参数。
实现
async setToken() {
/* let data = {
grant_type: 'client_credentials',
client_id: '62vwjyr7tdeizy0dx4',
client_secret: 'caf3a9aa8a28432aa36a18b12fa6fdcc',
} */
const params = new URLSearchParams();
params.append('grant_type', 'client_credentials');
params.append('client_id', '62vwjyr7tdeizy0dx4');
params.append('client_secret', 'caf3a9aa8a28432aa36a18b12fa6fdcc');
this.$axios({
method: 'post',
url: '/spbms/accessToken',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;'
},
data: params,
}).then(res => {
if(res.data.access_token){
//console.log(res.data.access_token)
localStorage.setItem('accessToken',res.data.access_token)
}
})
},
//封装接口情况下
export function getToken(){
const params = new URLSearchParams();
params.append('grant_type', 'client_credentials');
params.append('client_id', '62vwjyr7tdeizy0dx4');
params.append('client_secret', 'caf3a9aa8a28432aa36a18b12fa6fdcc');
return request.post('/spbms/accessToken',
params,
{header:{'Content-Type': 'application/x-www-form-urlencoded;'}}
);
}