Axios是一个基于Promise的HTTP客户端,用于浏览器和Node.js。它提供了一组简洁且易于使用的API,用于发送HTTP请求。下面是Axios的所有请求方法及其示例:
get请求:
获取数据
axios.get('/api/users')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
post请求:
提交数据(表单提交+文件上传)
axios.put('/api/users/1', {
name: 'John Doe',
age: 26
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
put请求:
更新数据(将所有数据均推放到服务端)
axios.post('/api/users', {
name: 'John Doe',
age: 25
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
patch请求:
部分更新资源
PATCH /api/users/123 HTTP/1.1
Host: example.com
Content-Type: application/json
{
"name": "John Doe",
"age": 30
}
delete请求:
删除数据
axios.delete('/api/users/1')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
head请求:
获取资源的头部信息
axios.head('/api/users')
.then(function (response) {
console.log(response.headers);
})
.catch(function (error) {
console.log(error);
});
options请求:
用于获取服务器支持的HTTP方法。
axios.options('/api/users')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});