• vue.js的ajax和jsonp请求


    首先要声明使用ajax 在 router下边的 Index.js中

    import VueResource from 'vue-resource';

    Vue.use(VueResource);

    ajax 和 jsonp 使用方法:

    //在Vue实例类使用  
    this.$http.get(url, [options]).then(successCallback, errorCallback);  
      
    var test = new  Vue({  
      el:'#v',  
      data:{  
        jsonUrl:'xxxx',  
        jsonpUrl:'xxxxx',  
        req:{}  
        resData:[]  
      },  
      mthods:{  
        init:function(id){  
          this.$http.get(this.jsonUrl,this.req).then(function(res){  
            console.log(res);  
            this.$set('resData',res);  
          },function(res){  
            console.warn(res);  
          })  
        },  
        cli:function(id){  
          //jsonp请求  
          this.$http.jsonp(this.jsonpUrl).then(  
            function(res){  
              console.log(res);  
              this.$set('resData',res);  
            }  
          )  
        }  
      }  
    })  

    //需要注意的是jsonp请求的服务端返回的数据格式有些不一样,下面以PHP为例 

    [php] view plain copy
    $call
    = $_GET['callback']; $json = json_encode(['data'=>'tttttt']); echo $call.'('.$json.')';

     

    vue请求数据方法

    1、get 请求并传递参数方法

    var param = {
                    page: this.page,
                    pageSize: this.pageSize,
                    sort: 1
                };
                this.$http.get('/goods',{
                    params: param
                }).then((response) => {
                    response = response.body.result.list;
                    this.shopList = response;
                });

    2、原始的拼接参数方法

    this.$http.get('/goods?page='+this.page+'&pageSize='+this.pageSize+'&sort=1').then((response) => {
                    console.log(response)
                    response = response.body.result.list;
    
                    this.shopList = response;
                    console.log(this.shopList)
                });

    3、post的请求

     this.$http.post('in.php',{a:1,b:2},{emulateJSON:true}).then( (res) => {
                           console.log(res.data)
                     } )

     

  • 相关阅读:
    实时计算平台构建
    Windows操作系统下VirtualBox开机自启动软件和虚拟机
    MacOS系统下VirtualBox开机自启动软件和虚拟机
    Maven命令,添加忽略SSL证书校验
    三层架构之业务层逻辑层
    Web网站开发中,Cookie是什么?
    三层架构之数据访问层
    三层架构之模型层
    ASP.NET与.NET Framework和C#的关系
    三层架构之表现层
  • 原文地址:https://www.cnblogs.com/haonanZhang/p/7110350.html
Copyright © 2020-2023  润新知