在使用 Axios 发送请求时,我们可以通过使用参数来传递数据。下面是一个使用 Axios 发送 GET 请求的示例:
import axios from 'axios';
axios.get('/api/data', {
params: {
id: 123,
name: 'Peter'
}
})
.then(response => console.log(response))
.catch(error => console.log(error));
在这个示例中,params 参数用于传递查询字符串参数,其中 id 和 name 是参数名称,123 和 Peter 是参数值。
我们还可以使用 data 参数来传递请求正文数据。例如,在使用 Axios 发送 POST 请求时,我们可以这样做:
import axios from 'axios';
axios.post('/api/data', {
id: 123,
name: 'Peter'
})
.then(response => console.log(response))
.catch(error => console.log(error));
在这个示例中,我们使用 data 参数传递请求正文数据。Axios 会自动将这些数据序列化为 JSON 字符串,并将其添加到请求正文中。
总的来说,Axios 提供了很多参数选项,可以使我们更方便地控制发送请求。通过仔细阅读 Axios 的文档,我们可以更好地了解这些参数的作用和用法。