1. 程式人生 > >axios 將post請求時把物件obj資料轉為formdata

axios 將post請求時把物件obj資料轉為formdata

今天在對後臺介面的時候,上傳報名資訊,利用axios 的post請求,發資料,


但是顯示引數錯誤,但是檢視header的時候,發現居然是一個物件:


這可不行,然會就想著怎麼把物件搞成formdata,直接加入兩段程式碼就好,

 transformRequest: [function (data) {
                let ret = ''
                for (let it in data) {
                  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
                return ret
              }],
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
              }

加在請求裡面,


最後在header檢視的時候,便是成功了,