对象数组作为请求 POST axios 的参数

FeR*_*cHo 3 arrays ajax json vue.js axios

我正在使用 vue.js 处理一个项目并处理我AJAX使用的请求Axios,我想知道是否可以将POST这种类型的对象数组作为参数传递给请求:

[{id: 1, name: 'max'}, {id: 2, name: 'jhon'}, {id: 3, name: 'anna'}]
Run Code Online (Sandbox Code Playgroud)

如果可能,最好的方法是什么?

rol*_*oli 6

当然!

let arrOfObj = [
  { name: 'John', lastName: 'Doe' },
  { name: 'Jane', lastName: 'Doe' }
]

axios.post('url_here',arrOfObj)
.then(console.log)
.catch(console.log)
Run Code Online (Sandbox Code Playgroud)


小智 6

是的,很有可能

let data = [
    {id: 1, name: 'max'}, 
    {id: 2, name: 'jhon'}, 
    {id: 3, name: 'anna'}
];

let formdata = new FormData();
formdata.append('data',JSON.stringify(data));

axios.post('/url`',formdata)
.then(res => console.log(res))
.catch(err => console.log(err)
Run Code Online (Sandbox Code Playgroud)

在接收端(假设是 PHP & Laravel)

$data = json_decode($request->data);
Run Code Online (Sandbox Code Playgroud)

然后循环遍历 $data 因为它是一个普通数组