AXIOS 入门

01-Axios 简介

Axios (Ai ke C U S , ajax i/o system), Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中;客户端向服务 器端发送异步请求。 Vue 官方提倡使用 Axios 来操作 AJAX 异步请求。
官网地址: axios中文网|axios API 中文文档 | axios

特点:

  • 支持浏览器和 node.js
  • 能拦截请求和响应
  • 能转换请求和响应数据
  • 能取消请求
  • 自动转换 JSON 数据
  • 浏览器端支持防止 CSRF ( 跨站请求伪造 )

1.1 项目添加 axios

1)下载

// 浏览器直接访问下载地址然后保存
https://unpkg.com/axios/dist/axios.min.js
2)然后把下载的文件放在自己的项目中,然后在需要使用的页面引入
<script src="static/vue/axios.min.js"></script>

02-发送AJAX请求

2.1 Get请求

1)语法
axios.get(url [, cong] );

axios.get('user/list').then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
2)传参

1\] 路径传参 > axios.get("goods/list?pageNum="+this.p.pageNum+"\&pageSize="+this.p.pageSize).then(function (response){ > this.vm.page=response.data; > }).catch(function (error) { > //alert("获取数据失败"); }) \[2\] params 传参 > axios.get("brand/list",{ > params:{ > cateId:this.goodssign.cateId, > } > }).then(function (response) { > this.vm.listthreesign=response.data; > }).catch(function (ee) { > alert("品牌加载失败"); }) ### **2.2 Post 请求** 1)语法 axios.post(url\[, data\[, cong\]\]); > axios.post('user/list').then(function (response) { > console.log(response); > }).catch(function (error) { > console.log(error); }); 2)传参 \[1\] 名 - 值对,自己拼接 > axios.post("goods/delete","id="+id).then(function (response) { > console.log(response); > location.reload(); > }).catch(function (ed) { > alert("删除失败"); }) \[2\] 当参数已经封装 js 对象,可以使用 qs.js 转化 > axios.post("goods/list",Qs.stringify(this.pagetwo)).then(function (response) { > this.vm.page=response.data; > }).catch(function (error) { > alert("页面加载失败"); }) 导入 qs \