什么是 axios?axios与promise区别

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中

promise 是现代 javascript 中异步编程的基础,是一个由异步函数返回的可以向我们指示当前操作所处的状态的对象

使用 cdn:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

// 上面的请求也可以这样做
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});


axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});


文章来自 www.96net.com.cn

posted @ 2024-02-14 12:35  学无边涯  阅读(86)  评论(0编辑  收藏  举报