简体   繁体   English

Axios 发送表格数据 controller 返回 object object

[英]Axios send formData in controller return object object

//Vuejs2 //Laravel v7.x //Vuejs2 //Laravel v7.x

I turn to you because I'm blocking, I can't find a solution.我求助于你,因为我正在阻止,我找不到解决方案。 I want to recover the data in my object in my controller.我想在我的 controller 中恢复我的 object 中的数据。 In my View.vue I make an axios post在我的 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);
                    });
        }
    }

I collect like that in my controller我在我的 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]
}

Thx for help.谢谢帮助。 have a nice day.祝你今天过得愉快。

You can't use .append to add objects to your FormData.您不能使用.append将对象添加到您的 FormData。

Looking at https://developer.mozilla.org/en-US/docs/Web/API/FormData/append the method only accepts an USVString or Blob as the value.查看https://developer.mozilla.org/en-US/docs/Web/API/FormData/append该方法仅接受 USVString 或 Blob 作为值。 Everything else is casted to String.其他所有内容都转换为字符串。

And the string representation of a standard object in Javascript is [object Object] . Javascript 中标准 object 的字符串表示是[object Object]

You can try JSON.stringify(this.customer) to convert it to its JSON representation.您可以尝试JSON.stringify(this.customer)将其转换为 JSON 表示。

Try $request->get('customer') or $request->input('customer')尝试 $request->get('customer') 或 $request->input('customer')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 formData 和 axios 发送嵌入式 Javascrippt Object - How to Send an embedded Javascrippt Object using formData and axios Axios FormData()获取空对象 - Axios FormData() getting empty Object Symfony5 - 表格数据 object 由 javascript 发送 - Symfony5 - formData object send by javascript XMLHttpRequest not Submited in controller 是否可以将文件列表和FormData对象中的整个json对象发送到ASP.NET CORE MVC控制器 - Is it possible to send a list of files and a whole json object in a FormData object to a ASP.NET CORE MVC Controller 如何使用 Axios JS 在数组中传递一个 formData 对象和另一个对象 - How to pass a formData object and another object inside array with Axios JS FormData中的对象数组将0个对象传递给控制器 - Object array in FormData passing 0 objects to controller 我正在尝试使用 axios 表单数据将 js 数组发送到 php 但它一直失败 - im trying to send a js array to php using axios formdata but it keep on failing i get [objext object] at console and if im trying to use stringify "" 如何使用 superagent 发送 FormData 对象 - How to use superagent to send a FormData object 如何将 Map Object With FormData 发送到 .NET API? - How to send a Map Object With FormData to .NET API? Ajax:如何在 json object 内部发送 FormData - Ajax: How to send FormData inside of json object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM