axios传递对象数组
时间: 2023-07-25 22:48:40 浏览: 41
要使用 Axios 传递对象数组,可以使用以下步骤:
1. 在发送 POST 请求时,将请求体设置为对象数组的 JSON 格式字符串。
```javascript
const data = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 }
];
axios.post('/api/users', JSON.stringify(data))
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
```
2. 在服务器端,使用 body-parser 中间件来解析请求体中的 JSON 数据。
```javascript
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/api/users', (req, res) => {
const data = req.body;
console.log(data);
// Handle the data...
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```
这样,你就可以使用 Axios 传递对象数组,并在服务器端接收和处理这些数据了。