axios发送请求
Axios是一个基于Promise的HTTP客户端,可以用于浏览器和Node.js中。通过Axios可以发送HTTP请求并处理响应。下面是Axios发送请求的示例代码:
import axios from 'axios';
// 发送GET请求
axios.get('/api/user')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
// 发送POST请求
axios.post('/api/user', {
firstName: 'John',
lastName: 'Doe'
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
在上面的示例中,我们分别使用Axios发送了GET和POST请求。在发送请求时,我们可以使用Axios提供的各种配置选项,如设置请求头、设置请求超时时间等。Axios还提供了一些拦截器,我们可以在请求发送前或响应返回后进行拦截并进行一些处理。Axios的使用非常灵活,可以满足各种不同的HTTP请求场景。
原文地址: https://www.cveoy.top/t/topic/Oeh 著作权归作者所有。请勿转载和采集!