//get请求 需要向后台传参数 语法:{params:{a:10,b:2}}
//this.$http.get(‘get.php’,{params:{a:10,b:2}}).then(function(res){}
this.$http.get(‘get.php’,{params:{a:10,b:2}}).then(function(res){
console.log(res);
console.log(res.data);
},function(res){
console.log(‘失败’)
})
}
}
})
get.php文件:
<?php $a=$\_GET\['a'\]; $b=$\_GET\['b'\]; echo $a-$b; ?>get方法有两个参数get(“PHP文件”,“参数”)
post方法:
<button @click=‘post()’>点击
post.php:
<?php $a=$\_POST\['a'\]; $b=$\_POST\['b'\]; echo $a+$b; ?>post方法有三个参数post(“php文件”,“参数”,“emulateJSON:true”
- emulateJSON:true 模拟一个JSON数据发送到服务器,这样才可以成功运行
这样放在服务器环境下就可以运行了,我用的是phpstudy
=======================================
模拟接口数据—显示到页面上
data.json数据
{
“total”:“4”,
“data”:[
{
“name”:“三国演义”,
“category”:“文学”,
“desc”:“一个军阀混战的年代”
},{
“name”:“水浒传”,
“category”:“文学”,
“desc”:“草寇or英雄好汉”
},{
“name”:“西游记”,
“category”:“文学”,
“desc”:“妖魔鬼怪牛鬼蛇神什么都有”
},{
“name”:“红楼梦”,
“category”:“文学”,
“desc”:“一个封建王朝的缩影”
}
],
“obj”:{“adf”:“adf”}
}
解析数据 get请求
<button @click=‘get()’>点击请求显示数据
- {{item.name}} {{item.desc}}
显示页面效果:
可以练习显示数据: