axios裡的post特別大的一個坑。

來源:互聯網
上載者:User

標籤:序列化   data   function   var   封裝   his   str   set   引入   

在jQuery中,ajax請求發送post很方便的,但是在vue項目中,引入jQuery就不再適合了。

var _this = thisvar data = {    filter:‘-_id‘}$.ajax({        url:‘http://localhost:3000/api/goods/get‘,        type:‘post‘,        data:data,        dataType:‘json‘,        success:function(res){          _this.msg =res.data        }      )

而在axios中的post請求要非常注意兩個地方:

要設定合適的要求標頭,一般採用x-www-form-urlencoded即可

發送的資料要序列化,特別注意啊,因為axios預設的格式是Request Payload。

axios.post(‘http://localhost:3000/api/goods/get‘,qs.stringify(data),{        headers: {            ‘Content-Type‘:‘application/x-www-form-urlencoded; charset=UTF-8‘          }      }).then(res=>{        _this.msg = res.data      },err =>{        console.log(err)      })

其中qs模組是一件封裝進axios模組裡了,

引入axios時同時引入qs即可。

如:

import axios from ‘axios‘import qs from ‘qs‘

  

axios裡的post特別大的一個坑。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.