ajax请求插件vue-resource的学习

  • 1.安装
    npm install vue-resource

  • 2.使用

        import VueResource from 'vue-resource';
        Vue.use(VueResource);
    
  • 3.语法

    • get请求:
    this.$http.get("someurl").then(
            function (res) {
                // 处理成功的结果
                alert(res.body);
            },function (res) {
            // 处理失败的结果
            }
        );
    
    • post请求:
    this.$http.post("someurl",{name:"zhangsan"},{emulateJSON:true}).then(
            function (res) {
                // 处理成功的结果
                alert(res.body);
            },function (res) {
            // 处理失败的结果
            }
        );
    
    • 说明:post请求传送给后台的数据必须有,{emulateJSON:true}也必须有,不然会出问题
    • jsonp请求:
    this.$http.jsonp("someurl").then(
            function (res) {
                // 处理成功的结果
                alert(res.body);
            },function (res) {
            // 处理失败的结果
            }
        );
    
原文地址:https://www.cnblogs.com/wan-fei/p/8351499.html