axios各种方法的写法
时间: 2023-10-14 21:56:11 浏览: 34
axios是一个用于发送HTTP请求的JavaScript库。它提供了多种方法来执行不同类型的请求。以下是一些常用的axios方法及其写法示例:
1. 发送GET请求:
```javascript
axios.get('/api/users')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
2. 发送POST请求:
```javascript
axios.post('/api/users', { name: 'John', age: 30 })
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
3. 发送PUT请求:
```javascript
axios.put('/api/users/1', { name: 'John', age: 35 })
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
4. 发送DELETE请求:
```javascript
axios.delete('/api/users/1')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
这些示例展示了基本的axios方法的写法。你可以根据实际需求,传递不同的URL、数据和配置参数来执行不同类型的请求。同时,你还可以使用axios的拦截器、并发请求等功能来进一步定制和优化你的HTTP请求。