vue中使用axios,实现前后台的交互,请求数据
1.安装axios依赖

cnpm install axios

2.在main.js中引入和挂载
引入

import axios from 'axios'

挂载组件

Vue.prototype.axios = axios;

3.在main.js委托

if(isDev){
  Vue.prototype.*BaseUrl* = '   ';
}else{
  Vue.prototype.*BaseUrl* = '   ';//空格是委托地址
}

好了开始使用
1.get请求(不传值的情况)

var _this=this;
_this.axios.post(this.*BaseUrl*+"请求的地址",{			
}).then(function(response){
	console.log(response)//捕获正确信息
}).catch(function(response){
	console.log(response)//捕获错误信息
})

2.post请求(传值的情况)

let msg= {
 	name:this.doctor_name,
};//组织数据,其中name是后台要求的参数名,this.doctor_name是前台传的数据	
this.axios.post(this.BaseUrl+"/请求地址",this.$qs.stringify(msg)).then(function(response){
	console.log(response)//捕获正确信息
}).catch(function(response){
	console.log(response)//捕获错误信息
})

其中qs与引入挂载axios一样放到main.js中

import qs from 'qs'
Vue.prototype.$qs = qs;

结束!新手,如有错误欢迎指正

Logo

前往低代码交流专区

更多推荐