1. 程式人生 > >Vue:axios中POST請求傳參問題

Vue:axios中POST請求傳參問題

color catch str ios 接收 django框架 nsf head con

vue的ajax請求用的是axios組件,結果在用到post請求的時候,發現給後臺傳data時,後臺(python語言+django框架)接收不到。

後臺的request.body顯示出,我給傳送的是data被django打包成了一個obj中的key值,value為空數組。導致後臺獲取不到。

解決方案:

axios({
    method:‘POST‘,
    url:ap_service_url+opt.url,
    data:opt.obj,
    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"}
}).then(function(res){
      /*請求成功*/
}).catch(function(err){
       /*請求失敗*/
})

通過添加transformRequset成功跳出這個坑~

Vue:axios中POST請求傳參問題