Vue vue-resource实现get-post-jsonp请求

get请求

当发起get请求之后,通过 .then 来设置成功的回调函数
通过result.body拿到服务器返回的成功的数据

<div id="app">
    <input type="button" value="get请求" @click="getInfo">
</div>
<script src="../js/vue.js"></script>
<script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{},
        methods:{
            getInfo(){
                this.$http.get('http://www.liulongbin.top:3005/api/getlunbo').then(function (result) {
                    console.log(result)
                })
            }
        }
    })
</script>

在这里插入图片描述

post请求

手动发起的post请求,默认没有表单格式,所以有的服务器处理不了
通过post方法的第三个参数,{emulateJson:true}设置提交的内容类型为普通表单数据格式

  postInfo() {
                this.$http.post('http://www.liulongbin.top:3005/api/post',{},{emulateJson:true}).then(function (result) {
                    console.log(result.body)
                })
            }

jsonp请求

返回的是一个json 解析就好了

jsonptInfo(){
                this.$http.jsonp('http://www.liulongbin.top:3005/api/jsonp').then(function (result) {
                    console.log(result.body)
                })
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值