1.安装axios,npm install --save axios
2.在main.js中引入
import axios from ‘.axios’
Vue.prototype.
a
x
i
o
s
=
a
x
i
o
s
/
/
如
果
V
u
e
.
p
r
o
t
o
t
y
p
e
.
axios=axios //如果Vue.prototype.
axios=axios//如果Vue.prototype.ajax=axios ,调用时写法如下②
在想要调用接口数据的页面中,get后面的地址是你的接口地址
methods:{
getData:function(){
this.$axios.get("https://api.apiopen.top/getSingleJoke?sid=28654780").then(res=>{
alert(res.data.result.text);
this.data=res.data.result.text;
console.log(res.data)
})
}
}
mounted(){
this.$ajax({
method:'get',
url:'https://api.apiopen.top/getSingleJoke?sid=28654780'
}).then(response=>{
alert(response.data.result.text)
}).catch(function(err){
console.log(err)
})
},
post请求:
var qs=require('qs')
var url="https://www.apiopen.top/weatherApi";
this.$axios.post(url,qs.stringify({
city:'青岛'
})).then(res=>{
alert(res.data.data.city)
console.log(res.data)
})