[英]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.