前言:作为一个后端开发的人来讲,使用vue也会写一些简单的按钮和请求,但遇到了一次
'Content-type': 'application/x-www-form-urlencoded;charset=utf-8' 的post请求让我搞了好久,主要是前端传参的问题,后端获取不到,原因是前端传参的方式与 applecation/json 参数的写法不一样,需要使用URLSearchParams对象去传参。
解决方案如下:
methods: {
getToken: function(code) {
("getToken(code)"+code)
const params = new URLSearchParams() // 创建对象
('grant_type', "authorization_code") // 封装对象
('code', ()) // 封装对象
('redirect_uri', "http://localhost:3000") // 封装对象
('client_id', "geecg") // 封装对象
('client_secret', "geecg") // 封装对象
axios({
method: "post",
url: 'http://localhost:48080/admin-api/system/oauth2/token',
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"tenant-id":"1"
},
data:params,
}).then(function (response) {
("111111111111")
(response);
}).catch(function (error) {
(error);
});
}
}