Vue-Resource基础介绍

vue-resource的请求API是按照REST风格设计的,它提供了7种请求API:

  • get(url, [options])
  • head(url, [options])
  • delete(url, [options])
  • jsonp(url, [options])
  • post(url, [body], [options])
  • put(url, [body], [options])
  • patch(url, [body], [options])

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue-resource</title>
   <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <script src="../../node_modules/"></script>
  <script src="../../node_modules/vue-resource/dist/vue-resource.js"></script>
</head>
<body>
<div id="app" class="container">
  <h2>vue-resource插件讲解</h2>
  <a href="javascript:;" class="btn btn-primary" @click="get">发送Get请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="post">发送Post请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="jsonp">发送Jsonp请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="http">发送htttp请求</a>
</div>
<script type="text/javascript">
  new Vue({
    el: "#app",
    data: {

    },
    mounted: function () {
      // 全局拦截器
      Vue.http.interceptors.push(function (request, next) {
        console.log("request init");
        next(function (response) {
          console.log("response init.");
          return response;
        });
      });
    },
    http:{
      // 对全局路径的配置
      root: "api/"
    },
    methods: {
      get: function () {
        this.$http.get("http://www.imooc.com/course/AjaxCourseMembers?ids=796",{
          params: {
            userId: "101"
          },
          headers: {
            token: "abcd"
          }
        }).then(res=>{
          this.msg = res.data;
        },error=>{
          this.msg = error;
        })
      },
      post: function () {
        this.$http.post("package.json",{
          userId: "102"
        },{
          headers:{
            access_token: "abc"
          }
        }).then(function (res) {
            this.msg = res.data;
        });
      },
      jsonp: function () {
        this.$http.jsonp("http://www.imooc.com/course/AjaxCourseMembers?ids=796").then(function (res) {
            this.msg = res.data;
        })
      },
      // 以配置的方式调用
      http: function () {
        this.$http({
          url: "package.json",
          params: {
            userId: "103"
          },
          headers: {
            token: "123"
          },
          timeout: 5,
          before: function () {
            console.log("before init")
          }
        }).then(function (res) {
          this.msg = res.data;
        });
      }
    }
  });
</script>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值