vue-resource 实现 get, post

在这里插入图片描述

vue-resource 实现 get, post

1.除了 vue-resource 之外,还可以使用 axios 的第三方包实现实现数据的请求

vue-resource下载地址https://github.com/pagekit/vue-resource

npm

npm install vue-resource  

cdn

<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>

2.vue-resource 的配置步骤:

  • 直接在页面中,通过script标签,引入 vue-resource 的脚本文件;

  • 注意:引用的先后顺序是:先引用 Vue 的脚本文件,再引用 vue-resource 的脚本文件;

    3.发送get请求:

//				请求地址				请求数据		then(成功function,失败function)
this.$http.get('http://127.0.0.1:4000/?'+'username=123').then(function (result) {
    console.log(result.body)
    console.log('请求成功');
},function (err) {
    console.log('请求失败');
})

4.发送post请求:

postInfo() {
    var url = 'http://127.0.0.1:8899/api/post';
    // post 方法接收三个参数:
    // 参数1: 要请求的URL地址
    // 参数2: 要发送的数据对象
    // 参数3: 指定post提交的编码类型为 application/x-www-form-urlencoded
    this.$http.post(url, { name: 'zs' }, { emulateJSON: true }).then(function(data){
        console.log(data.body);
        console.log('post请求成功');
    },function(err){
        console.log('post请求失败');
    })
}

5.其他请求参照api

https://vue-resource.changwei.me/http请求与响应.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值