axios结合vue使用
<html>
<head>
<title>
axios网络请求
</title>
</head>
<body>
<div id="app">
<input type="button" value="获取笑话" @click="getJoke">
<p>
{{ joke }}
</p>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!--引入axios包-->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
var app = new Vue({
el: "#app",
data: {
joke: ""
},
methods: {
getJoke:function(){
var that = this;
axios.get("https://autumnfish.cn/api/joke").then(function(response){
// this会变的,需要优先保存起来
that.joke = response.data;
},function(err){
console.log(err)
})
}
}
})
</script>