经常使用jquery的ajax。现在想使用vue了,但是发现axios的参数传递的时候不是FormData的。 应为我需要用到__RequestVerificationToken所以找到必须使用transformRequest进行参数的转换。特此记录。感觉vue还是不错的。
axios({
method: 'post',
url: '/BaseData/GetOrganization',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
transformRequest: [
function (data) {
let ret = ''
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
ret = ret.substring(0, ret.lastIndexOf('&'));
return ret
}
],
data: {
__RequestVerificationToken: token,
ID: ID,
ParentID: ParentID,
OrganizationName:'easyboot'
}
// responseType: 'stream'
})
.then(function (response) {
console.log(response)
//response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});
本文详细介绍了在Vue项目中如何使用axios发送FormData格式的POST请求。通过自定义transformRequest函数,实现将数据转换为应用/x-www-form-urlencoded格式,以便在后端能够正确解析__RequestVerificationToken等关键参数。
214

被折叠的 条评论
为什么被折叠?



