vue3 axios传参,后端怎样接收
时间: 2023-05-27 16:04:39 浏览: 149
在Vue3中使用Axios发送请求时,可以通过以下两种方式来传递参数:
1. 使用params参数传递参数:
```js
axios.get('/api/data', {
params: {
id: 1,
name: 'John'
}
})
```
此时,后端可以通过req.query来接收参数,如下:
```js
app.get('/api/data', (req, res) => {
const id = req.query.id
const name = req.query.name
// do something
})
```
2. 使用data参数传递参数:
```js
axios.post('/api/data', {
id: 1,
name: 'John'
})
```
此时,后端可以通过req.body来接收参数,如下:
```js
app.post('/api/data', (req, res) => {
const id = req.body.id
const name = req.body.name
// do something
})
```
需要注意的是,使用data参数时,需要在请求头中设置content-type为'application/json'。如下:
```js
axios.post('/api/data', {
id: 1,
name: 'John'
}, {
headers: {
'content-type': 'application/json'
}
})
```
否则,后端无法正确解析请求数据。
阅读全文
相关推荐


















