首页 > Web开发 > 详细

vue.js的ajax和jsonp请求

时间:2017-07-03 13:17:46      阅读:394      评论:0      收藏:0      [点我收藏+]

首先要声明使用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.js的ajax和jsonp请求

原文:http://www.cnblogs.com/haonanZhang/p/7110350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!