Axios整理(vue 数据请求)

本文介绍了Axios的使用,包括安装、引入加载、请求方式、全局默认值、拦截器及跨域处理等内容,还提到跨域测试阶段的解决办法。同时阐述了Mock数据模拟的几种方式,如创建JSON文件、集成服务器、使用线上数据和数据模拟库,并介绍了MockJS的语法。

Axios

  中文:https://www.kancloud.cn/yunye/axios/234845 

  github:https://github.com/axios/axios

  1.安装

    npm install axios

  2.引入加载

    import Axios from "axios"

    Vue.prototype.$axios = Axios

  3.请求

    get请求:

this.$axios("http://www.wwtliu.com/sxtstu/news/juhenews.php",{
	params:{
		 type:"junshi",
		 count:30
	}
})
.then(res => {
	 this.newsData = res.data;
	 console.log(res.data);
})
.catch(error => {
	 console.log(error);
})

    post请求:

      form-data:?name=iwen&age=20

      x-www-form-urlencoded:{name:"iwen",age:20}

      注意:axios接受的post请求参数的格式是form-data格式

this.$axios.post("http://www.wwtliu.com/sxtstu/blueberrypai/login.php",	qs.stringify({
	user_id:"iwen@qq.com",
	password:"iwen123",
	verification_code:"crfvw"
}))
.then(res => {
	console.log(res.data)
})
.catch(error => {
	console.log(error);
})

  4.全局的 axios 默认值

	axios.defaults.baseURL = 'https://api.example.com';
	axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
	axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

  5.拦截器

  6.跨域处理:

    修改config index.js文件

proxyTable: {
	       "/api": {
	                target: "http://localhost:3000",
	                changeOrigin: true,
	                pathRewrite: {
	                    '^/api': ''
	                }
	        }
}

   添加host

     Vue.prototype.HOST = '/api'

   注意:此种跨域解决方案,只能适用于测试阶段,打包的时候,不会具备服务器

   不能跨域了,后端解决。

Mock:数据模拟

  1.自己创建JSON文件。使用get请求形式访问数据

    优点:方便,快捷

    缺点:只能存在get请求

  2.项目中集成服务器,模拟各种接口

    优点:模拟真实线上环境

    缺点:增加开发成本

  3.直接使用线上数据

    优点:真实

    缺点:不一定每个项目都存在

  4.数据模拟库

    http://mockjs.com/

MockJS:

  语法:

    'list|1-10': [{

      'id|+1': 1

    }]

    1.'name|1': array

    从属性值 array 中随机选取 1 个元素,作为最终值。

    2.'name|+1': array

    从属性值 array 中顺序选取 1 个元素,作为最终值。

    3.'name|min-max': array

    通过重复属性值 array 生成一个新数组,重复次数大于等于 min,小于等于 max。

    4.'name|count': array

    通过重复属性值 array 生成一个新数组,重复次数为 count。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值