axios
vue2.0 推荐使用axios
npm install axios
//调用const baseUrl = "https://www.fastmock.site/mock/ad533d42207a36676b39a3d5cd8dfe5f/api";axios.get(`${baseUrl}/backlist`).then((res)=>{this.tableData = res.data.tabledata //获取接口的表格数据this.oldTable = res.data.tabledatathis.$emit('Datatotal',this.tableData.length)//将导航栏总数设置为表格数据总数}).catch(error =>{console.log(error)})
执行多个并发请求
getUser(){return axios.get('/user')}getUsePermissions(){return axios.get('/user/xx/xx')}axios.all([getUser(),getUsePermissions()]).then(axios.spread(functiong(acct,perms){}))
axios API
axios({method:'post',url:'user/xx',data:{firstName:'111',lastName:'222'}});axios({method:'get',url:'user/xx',}).then(res=>{})
vue-resource
GET
window.onload = function(){var vm = new Vue({el:'#box',data:{msg:'Hello World!',},methods:{get:function(){//发送get请求this.$http.get('/ajax').then(function(res){document.write(res.body);},function(){console.log('请求失败处理');});}}});}
使用get 传递数据的时候 可以使用两个参数,后面那个参数是传给后端的
this.$http.get('get.php',{params : {a:1,b:2}}).then(function(res){document.write(res.body);},function(res){console.log(res.status);});
POST
post 发送数据到后端,需要第三个参数 {emulateJSON:true}。
emulateJSON 的作用: 如果Web服务器无法处理编码为 application/json 的请求,你可以启用 emulateJSON 选项。
this.$http.post('/ajax',{name:"xxx",url:"xxxxx"},{emulateJSON:true}).then(function(res){document.write(res.body);},function(res){console.log(res.status);});
