默认提交payload
以json形式提交
axios.post(url, {key: value} ).then( res => { console.log(res) }).catch( res => { console.log(res) })
提交formdata
如果后端人员比较老派,没法接受json格式,那就给他提交formdata
var fd = new FormData() fd.append('key', value) let config = { headers: { 'Content-Type': 'multipart/form-data' } } axios.post(url, fd, config ).then( res => { console.log(res) }).catch( res => { console.log(res) })
或者使用CDN方式引入
<html> <head> <!-- Axios --> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <!-- 引入Qs --> <script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script> </head> <body> <script> let url = "http://127.0.0.1" let data = { name: "Tom", age: 23 } // 注意Qs是大写,和npm引入方式不一样 axios.post(url, Qs.stringify(data)).then(res=>{ console.log(res); }).catch(err=>{ console.log(err); }) </script> </body> </html>
参考