[英]How to send axios post with application / x-www-form-urlencoded?
How to send axios post with application / x-www-form-urlencoded?如何使用应用程序 / x-www-form-urlencoded 发送 axios 帖子?
I need to send a refresh token, but when requested, an empty object is sent, although if you look in "userData.data.refresh_token"
, then the token is definitely there我需要发送一个刷新令牌,但是当请求时,一个空的 object 被发送,虽然如果你查看"userData.data.refresh_token"
,那么令牌肯定在那里
const params = new URLSearchParams();
const test = params.append("refresh_token", userData.data.refresh_token);
console.log(test) // undefined
console.log(userData) // token not empty
axios.post(`${API_URL}/api/login_check`, test, (res) => {
login(res);
});
Official link: https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format官方链接: https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
Below is from the link above.以下来自上面的链接。
In a browser, you can use the URLSearchParams API as follows:在浏览器中,您可以使用 URLSearchParams API,如下所示:
const params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);
Note that URLSearchParams is not supported by all browsers (see caniuse.com), but there is a polyfill available (make sure to polyfill the global environment).请注意,并非所有浏览器都支持 URLSearchParams(请参阅 caniuse.com),但有一个可用的 polyfill(确保对全局环境进行 polyfill)。
Alternatively, you can encode data using the qs library:或者,您可以使用qs库对数据进行编码:
const qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 }));
const axios = require('axios')
Try this code:试试这个代码:
/*... */ /*... */
const params = new URLSearchParams()
params.append('name', 'Akexorcist')
params.append('age', '28')
params.append('position', 'Android Developer')
params.append('description', 'birthdate=25-12-1989&favourite=coding%20coding%20and%20coding&company=Nextzy%20Technologies&website=http://www.akexorcist.com/')
params.append('awesome', true)
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
axios.post(url, params, config)
.then((result) => {
// Do somthing
})
.catch((err) => {
// Do somthing
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.