【发布时间】:2019-03-15 23:58:09
【问题描述】:
我有一个 Vuejs 组件的方法:
async submit () {
if (this.$refs.form.validate()) {
let formData = new FormData()
formData.append('userImage', this.avatarFile, this.avatarFile.name)
this.avatarFile = formData
try {
let response = await this.$axios.post('http://localhost:3003/api/test.php', {
avatar: this.avatarFile,
name: this.name,
gender: this.gender,
dob: this.DOB,
}, {
headers: {
'Content-Type': 'multipart/form-data; boundary=' + formData._boundary
}
})
if (response.status === 200 && response.data.status === 'success') {
console.log(this.response)
}
} catch (e) {
console.log(e)
}
}
}
在test.php 中,我使用json_decode(file_get_contents("php://input"), TRUE); 将数据作为$_POST 变量读取。
虽然我能够正确读取name、gender 和dob,但我无法正确读取avatar。
有相同的解决方案吗?
注意:我不会将每个变量都附加为formData.append(.., ..),因为我计划处理超过 14 个变量。
版主注意事项:我没有发现任何有关 formData 与其他数据对象一起使用的问题。
【问题讨论】:
-
我相信你必须在每个变量上调用
formData.append()。为什么会出现这样的问题?你不是在 axios 调用中声明它们吗?您只需在其他地方进行。 -
@acdcjunior 感谢您的提示
标签: php vue.js vue-component axios nuxt.js