Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。axios中文文档|axios中文网 | axios
特性
- 从浏览器中创建 XMLHttpRequest
- 从 node.js 创建 http 请求
- 支持 Promise API
- 拦截请求和响应
- 转换请求数据和响应数据
- 取消请求
- 自动转换 JSON 数据
- 客户端支持防御 XSRF
安装
cnpm install axios --save-dev
cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
GET请求
axios.get('index.php',{
params:{
id:12345
}
}).then((response)=>{
console.log(response)
}).catch((error)=>{
console.log(error)
})
POST请求
axios.post('index.php',{
id:12345
}).then((response)=>{
conso