首先要声明使用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.‘)‘;
原文:http://www.cnblogs.com/haonanZhang/p/7110350.html