javascript - Axios 在 Controller 返回对象对象中发送 formData

标签 javascript laravel vue.js vuejs2 axios

//Vuejs2
//Laravel v7.x
我求助于您,因为我正在阻止,我找不到解决方案。
我想在我的 Controller 中恢复对象中的数据。
在我的 View.vue 中,我发布了一个 axios 帖子

     data() {
        return {
          customer: {
             name: 'abc',
             login: 'def'
          },
          file: null
        }
    },methods: {
        submit(){
            let formData = new FormData();
            formData.append("customer", this.customer);
            formData.append("file", this.file);

            axios.post('/project/new',
                        formData, {
                            headers: {
                                "Content-Type": "multipart/form-data"
                            }
                        }).then(data => {
                        console.log(data.data);
                    });
        }
    }
我在我的 Controller 中收集这样的
public function postProject(Request $request)
{
    return $request->customer;  //return [Object Object]
    return $request->customer->name;  //return Trying to get property 'name' of non-object
    return $request->customer['name']; //return Illegal string offset 'name'
    return $request->file;  //return [Object Object]
}
谢谢你的帮助。祝你今天过得愉快。

最佳答案

您不能使用 .append将对象添加到您的 FormData。
看着 https://developer.mozilla.org/en-US/docs/Web/API/FormData/append该方法只接受 USVString 或 Blob 作为值。其他一切都被转换为字符串。
Javascript 中标准对象的字符串表示是 [object Object] .
你可以试试JSON.stringify(this.customer)将其转换为其 JSON 表示。

关于javascript - Axios 在 Controller 返回对象对象中发送 formData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63187188/

相关文章:

javascript - 如何防止剪贴板回车变成换行

php - 在 laravel 中更新记录时出现 MethodNotAllowedHttpException

laravel - Lumen/Laravel 从 Http header 获取参数值

javascript - 我如何可以有选择地在 v-bind 中包含 HTML 属性?

javascript - Firebase Auth 和 Vue-router

javascript - Div 内容可编辑,按 Enter 键时插入新元素

javascript - 动态添加和删除样式表

javascript - 如何在 IE 9 中调试 JavaScript?

javascript - IE9、IE11 和 Safari 的复制到剪贴板选项

php - Laravel 验证 "required_without"