1.安装axios:npm install axios,等待安装完毕即可
2.引用axios:在需要使用的页面中引用 import axios from 'axios' 即可
axios请求的时候有两种方式:一种是get请求,另一种是post请求
get请求:
- axios({
- method:"get",
- url: "", // 接口
- params: {
- // 参数
- name: xxx,
- },
- })
- .then(function (res) {
- console.log(res); // 成功回调
- })
- .catch(function (err) {
- console.log(err); // 失败回调
- });
post请求:
post请求还需要引入qs的文件 npm install qs 和 import qs from 'qs'
- axios({
- method:"post",
- url: "", // 接口
- data:qs.tostring{
- // 参数
- name: xxx,
- },
- })
- .then(function (res) {
- console.log(res); // 成功回调
- })
- .catch(function (err) {
- console.log(err); // 失败回调
- });
还有一些具体信息:
method:创建请求时使用的方法
url:请求的服务器地址
header:即将被发送的自定义请求头
data:请求接口所需要传递的参数
params:即将与请求一起发送的 URL 参数
then(function(succ) {}):成功时返回的数据
catch(function(err){}):失败时返回的信息