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

本文详细介绍如何使用Vue-resource库进行GET、POST和JSONP请求。通过示例代码展示了如何在Vue.js应用中发起不同类型的HTTP请求,并处理服务器响应。

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

  • 除了 vue-resource 之外,还可以使用 axios 的第三方包实现数据的请求
    <div id="app">
          <input type="button" value="get请求" @click="getInfo">
          <input type="button" value="post请求" @click="postInfo">
          <input type="button" value="jsonp请求" @click="jsonpInfo">
    </div>
    
    
    var vm = new Vue({
              el: '#app',
              data: {
                  
              },
              methods: {
                  getInfo() { // 发起 get 请求
                      // 当发起 get 请求之后,通过 .then 来设置成功的回调函数
                      this.$http.get('请求地址').then(function(result) { // 成功时的回调,必须写
                          // 通过 result.body 拿到服务器返回的成功的数据
                          console.log(result.body);
                      });
                  },
                  postInfo() { // 发起 post 请求   application/x-www-form-urlencoded
                 // 手动发起的 post 请求,默认没有表单格式,所以,有的服务器处理不了
                 // 通过 post 方法的第三个参数,{ emulateJSON: true } 设置提交的内容类型为普通表单数据格式
                      this.$http.post('请求地址',{},{ emulateJSON: true }).then(result=>{
                          console.log(result.body);
                      });
                  },
                  jsonpInfo() { // 发起 jsonp 请求
                      this.$http.jsonp('请求地址').then(result=>{
                          console.log(result.body);
                      });
                  }
              },
          });
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值