演示Vue之 axios
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="./lib/vue-2.6.12.js"></script>
<script src="./lib/axios.js"></script>
<title>axios</title>
</head>
<body>
<div style="margin: 5px;display:inline-block;background-color: burlywood;" title="axios">
<div id="app01">
<p>{{result_Data01}}</p>
<button @click="requestFun_01">发起请求</button>
<br />
<p>{{result_Data02}}</p>
<button @click="requestFun_02">get</button>
<br />
<p>{{result_Data03}}</p>
<button @click="requestFun_03">post</button>
</div>
<script>
new Vue({
el: '#app01',
data: {
request_method:'GET',
request_url_get : 'http://www.liulongbin.top:3006/api/getbooks',
request_url_post : 'http://www.liulongbin.top:3006/api/post',
request_id : 1 ,
request_name : '哈哈' ,
request_gender : '男' ,
result_Data01:'postFun_01目前无数据',
result_Data02:'postFun_02目前无数据',
result_Data03:'postFun_03目前无数据'
},
methods:{
async requestFun_01(){
const {data : res } = await axios({
method: this.request_method,
url: this.request_url_get,
params: {
id: this.request_id
},
data: {
},
})
this.result_Data01=res;
console.log(res);
},
async requestFun_02(){
const {data : res } = await axios.get(
this.request_url_get,
{
params: {
id: this.request_id
}
}
);
this.result_Data02=res;
console.log(res);
},
async requestFun_03(){
const {data : res } = await axios.post(
this.request_url_post,
{
name : this.request_name,
gender : this.request_gender
}
);
this.result_Data03=res;
console.log(res);
}
}
});
</script>
</div>
</body>
</html>