JSONP是利用Javascript可以跨域的特性从服务端请求数据的,即在跨域的情况下才有必要使用JSONP来发送请求。vue-resource提供了三种调用方式。
1.全局方法
Vue.http({
url:'http://example.com/books',
//参数部分,将会拼接在url后面
params:{
cat:1
},
method:'JSONP'
}).then(function(response){
//response.data为服务端返回的数据
console.log(response.data)
}).catch(function(response){
//出错处理
console.log(response)
})
2.实例底层方法
this.$http({
url:'http://example.com/books',
//参数部分,将会拼接在url后面
params:{
cat:1
},
method:'JSONP'
}).then(function(response){
//this指向当前组件实例
console.log(this)
}).catch(function(response){
//出错处理
console.log(response)
})
3.实例便捷方法
this.$http.jsonp(
'http://example.com/books',
//参数部分,将会拼接在url后面
{
cat:1
}
).then(function(response){
//this指向当前组件实例
console.log(this)
}).catch(function(response){
//出错处理
console.log(response)
})