axios 参数为payload的解决方法
生活随笔
收集整理的這篇文章主要介紹了
axios 参数为payload的解决方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 添加頭部headers
headers: {'Content-Type': 'application/x-www-form-urlencoded',}, axios.post(url, {a: 1, b:2}, {headers: {'Content-Type': 'application/x-www-form-urlencoded',}, }).then(response => response.data).then(err => {console.log(err);});?2. 在Browser環(huán)境下
?2.1 利用qs.stringify()處理參數(shù)
var qs = require('qs'); axios.post('/foo', qs.stringify({ 'bar': 123 }); <script src="/your-path/qs.min.js"></script> axios({url: url,method: 'post',data: Qs.stringify(params) }) .then(function (resp) { // }) .catch(function (err) { // })2.2 利用?URLSearchParams API 處理post參數(shù)
const params = new URLSearchParams(); params.append('param1', 'value1'); params.append('param2', 'value2'); axios.post('/foo', params);查看 URLSearchParams?兼容性,還可以使用pollify
?3. 在node環(huán)境下
可以使用querystring 模塊
const querystring = require('querystring'); axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));或者使用qs.stringify(),qs同樣可以在node中使用
4. 參考下面鏈接
https://github.com/mzabriskie/axios/blob/master/README.md#using-applicationx-www-form-urlencoded-format
轉(zhuǎn)載于:https://www.cnblogs.com/bldf/p/6366118.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的axios 参数为payload的解决方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。