使用axios发布文章,代码如下:

this.$axios({

method: 'post',

url: '/post',

data: {

description: opt.editorContent,

time: time,

...

},

})

.then(res => {

...

}

发送的请求是json格式的,后台常规方式无法获取,而且当description数据过大会报413 FULL head错误

手动改Content-Type也不起作用

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

回答:

更新:

刚刚在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参数传递,导致和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 => {

...

}

回答:

哈哈,这个我刚用axios时也遇到过,jq的ajax是默认 application/x-www-form-urlencoded 的,也就是已经加密过的,但是 axios 不是。

回答:

感谢分享,学习了,倒是一直没遇到过这个问题

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐