【问题标题】:send Array of object via axios post in laravel vue在 laravel vue 中通过 axios post 发送对象数组
【发布时间】:2021-05-22 19:26:09
【问题描述】:

我正在使用 vue 和 laravel。我在 console.log(array_of_obj) 中得到了这样的对象数组:

[…]
​
0: Object { id: Getter & Setter, employee_id: Getter & Setter, manual_id: Getter & Setter, … }
​
1: Object { id: Getter & Setter, employee_id: Getter & Setter, manual_id: Getter & Setter, … }
​
2: Object { id: Getter & Setter, employee_id: Getter & Setter, manual_id: Getter & Setter, … }

每个对象都包含我想要的行数据。现在我想通过 axios 发布请求将它发送到 Laravel 控制器。我怎样才能做到这一点? 我试过这种方式:

let json=JSON.stringify(array_of_obj);
let post_data={json_data:json}
 store.dispatch('employees/EmployeeListDownloadPdf', post_data).catch(err => { console.error(err) })

在我的 vuex 部分:

EmployeeListDownloadPdf(ctx, employeeList) {
                return new Promise((resolve, reject) => {
                    
                    axios({
                        url: '/api/employees/employeeListData',employeeList,
                        method: 'POST',
                        
                      })
                        //.then(response => resolve(response))
                        .then((response) => {
                            //console.log(response.data)
                            
                          });
                        //.catch(error => reject(error))
                })
            },  

但在我的控制器中,我尝试使用以下代码但得到空数组。

dd(json_decode($request->all(),true));
dd(json_decode($request,true));

我想将所有数据输入我的控制器,但现在无法访问它们。我该怎么做?

【问题讨论】:

  • 我认为您需要向我们展示 VueX 操作 employees/EmployeeListDownloadPdf 的作用。您分享的内容并未显示您的 VueJS 应用程序如何将数据发送到服务器。
  • 现已更新。请检查

标签: json laravel vue.js post axios


【解决方案1】:

您错误地使用了axios():如果您使用read the docs,则需要将employeeList 分配给对象中的数据属性,即:

axios({
    url: '/api/employees/employeeListData',
    data: employeeList,
    method: 'POST'
})

【讨论】:

  • 现在我可以在 Controller 中获取我的数据了。非常感谢。
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 2020-10-16
  • 2019-06-21
  • 2011-10-02
相关资源
最近更新 更多