1.axios #
- axios是一个基于 promise 网络请求库,作用于node.js 和浏览器中
- 它是 isomorphic 的(即同一套代码可以运行在浏览器和node.js中)
- 在服务端它使用原生 node.js http 模块, 而在客户端 (浏览端) 则使用 XMLHttpRequests
2.安装 #
$ npm install axios
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
3.基本用例 #
3.1 请求配置 #
url: '/users',
method: 'get',
baseURL: 'http://localhost:3000',
headers: {'Content-Type': 'application/json'},
params: {id: 1},
data: {name: 'zhufeng'},
timeout: 1000,
responseType: 'json',
}
3.2 响应结构 #
{
data: {},
status: 200,
statusText: 'OK',
headers: {},
config: {}
}
3.3 示例 #
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios({
method: 'post',
url: 'http://localhost:3000/users',
data: { name: 'New User', age: 20 }
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios({
method: 'put',
url: 'http://localhost:3000/users',
data: { name: 'Updated User', age: 21 },
params: { id: 1 }
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios({
method: 'get',
url: 'http://localhost:3000/users',
params: { id: 1 }
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios({
method: 'delete',
url: 'http://localhost:3000/users',
params: { id: 1 }
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
</script>
</body>
</html>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.post('http://localhost:3000/users', { name: 'New User', age: 20 })
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios.put('http://localhost:3000/users?id=1', { name: 'Updated User', age: 21 })
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios.get('http://localhost:3000/users?id=1')
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios.delete('http://localhost:3000/users?id=1')
.then(response => console.log(response.data))
.catch(error => console.error(error));
</script>
</body>
4.默认配置 #
4.1 全局 axios 默认值 #
axios.defaults.baseURL = 'http://localhost:3000';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/json';
4.2 自定义实例默认值 #
- 您可以使用自定义配置新建一个实例
- 指定的配置将与实例的配置合并
axios.create([config])
const instance = axios.create({
baseURL: 'http://localhost:3000',
timeout: 1000,
headers: {'Content-Type': 'application/json'}
});
instance.get(url[, config])
instance.delete(url[, config])
instance.head(url[, config])
instance.post(url[, data[, config]])
instance.put(url[, data[, config]])
4.3 配置的优先级 #
- 配置将会按优先级进行合并
- 它的顺序是:在lib/defaults.js中找到的库默认值,然后是实例的 defaults 属性,最后是请求的 config 参数。后面的优先级要高于前面的。
const instance = axios.create();
instance.defaults.timeout = 2500;
instance.get('/longRequest', {
timeout: 5000
});
5.拦截器 #
- 在请求或响应被 then 或 catch 处理前拦截它们
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
const instance = axios.create({
baseURL: 'http://localhost:3000',
timeout: 1000,
headers: { 'Content-Type': 'application/json' }
});
instance.interceptors.request.use(function (config) {
config.headers = { ...config.headers, 'Content-Type': 'application/json' };
return config;
}, function (error) {
return Promise.reject(error);
});
instance.interceptors.response.use(function (response) {
return response.data;
}, function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(error.config);
return Promise.reject(error);
});
instance.post('http://localhost:3000/users', { name: 'New User', age: 20 })
.then(response => console.log(response))
.catch(error => console.error(error));
instance.put('http://localhost:3000/users?id=1', { name: 'Updated User', age: 21 })
.then(response => console.log(response))
.catch(error => console.error(error));
instance.get('http://localhost:3000/users?id=1')
.then(response => console.log(response))
.catch(error => console.error(error));
instance.delete('http://localhost:3000/users?id=1')
.then(response => console.log(response))
.catch(error => console.error(error));
</script>
</body>