在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ axios post怎么使用?傳大量數(shù)據(jù)時,出現(xiàn)413 FULL head

axios post怎么使用?傳大量數(shù)據(jù)時,出現(xiàn)413 FULL head

使用axios發(fā)布文章,代碼如下:

this.$axios({
    method: 'post',
    url: '/post',
    data: {
        description: opt.editorContent,
        time: time,
        ...
    },
})
.then(res => {
    ...
}

發(fā)送的請求是json格式的,后臺常規(guī)方式無法獲取,而且當description數(shù)據(jù)過大會報413 FULL head錯誤
手動改Content-Type也不起作用

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
回答
編輯回答
眼雜

哈哈,這個我剛用axios時也遇到過,jq的ajax是默認 application/x-www-form-urlencoded 的,也就是已經(jīng)加密過的,但是 axios 不是。

2018年2月19日 10:33
編輯回答
兮顏

感謝分享,學習了,倒是一直沒遇到過這個問題

2018年7月25日 06:43
編輯回答
孤毒

更新:
剛剛在sf看到一個終極解決方案

// http request 攔截器
axios.interceptors.request.use(
  config => {
    if (config.method === 'post' || config.method === 'put') {
      config.data = qs.stringify(config.data)
      // config.headers['Content-Type'] = 'application/x-www-form-urlencoded' 可以不配置,會自動識別
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })

==================

這個問題卡了一天了,百度谷歌都搜了一圈了,issue里也有一些信息(都使用了qs.stringify),但都使用了別名方式:

axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));

記錄下解決方法:
按道理,使用post方式,應該使用['Content-Type'] = 'application/x-www-form-urlencoded'方式才對。。。為什么還是json類型的。
之前post一直是用params參數(shù)傳遞,導致和get請求一樣,變成了URL parameters跟在url里,導致了url太長,413 FULL head報錯了
我喜歡使用配置的方式寫axios,此時post需要在transformRequest里處理了,才能識別為x-www-form-urlencoded類型

this.$axios({
    method: 'post',
    url: '/post',
    data: {
        description: opt.editorContent,
        time: time,
        ...
    },
    transformRequest: [function (data, headers) {
        console.log(headers)
        return qs.stringify(data)
    }],
})
.then(res => {
    ...
}
2018年6月1日 08:56