admin管理员组文章数量:1335877
How to send axios post with application / x-www-form-urlencoded?
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
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);
});
How to send axios post with application / x-www-form-urlencoded?
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
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);
});
Share
Improve this question
asked May 19, 2021 at 6:41
reactchelreactchel
2643 gold badges4 silver badges11 bronze badges
2 Answers
Reset to default 3const 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&pany=Nextzy%20Technologies&website=http://www.akexorcist./')
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
})
Official link: https://github./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:
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.), but there is a polyfill available (make sure to polyfill the global environment).
Alternatively, you can encode data using the qs library:
const qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 }));
本文标签: javascriptHow to send axios post with applicationxwwwformurlencodedStack Overflow
版权声明:本文标题:javascript - How to send axios post with applicationx-www-form-urlencoded? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396710a2467072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论