vue $http请求服务

//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”

  1. 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}}
    • 显示页面效果:

      可以练习显示数据:

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值