1、Content-Type: application/json
import axios from 'axios' let data = {"code":"1234","name":"yyyy"}; axios.post(`${this.$url}/test/testRequest`,data) .then(res=>{ console.log('res=>',res); })
2、Content-Type: multipart/form-data
import axios from 'axios' let data = new FormData(); data.append('code','1234'); data.append('name','yyyy'); axios.post(`${this.$url}/test/testRequest`,data) .then(res=>{ console.log('res=>',res); })
3、Content-Type: application/x-www-form-urlencoded
import axios from 'axios' import qs from 'Qs' let data = {"code":"1234","name":"yyyy"}; axios.post(`${this.$url}/test/testRequest`,qs.stringify({ data })) .then(res=>{ console.log('res=>',res); })
總結:
1、從jquery轉到axios最難忘的就是要設置Content-Type,還好現在都搞懂了他們的原理
2、上面三種方式會對應后台的請求方式,這個也要注意,比如java的@RequestBody,HttpSevletRequest等等
作者:Awbeci
鏈接:https://segmentfault.com/a/1190000015261229
來源:SegmentFault 思否
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
axios中get請求與post請求的簡單函數封裝 - kylong - 博客園
https://www.cnblogs.com/kyl-6/p/9502779.html