vue-resource基本HTTP调用和RESTful调用action方法执行后都会返回一个Promise对象,该Promise对象提供了then、catch、finally等常用方法来注册回调函数。代码示例如下:
var promise=this.$http.post(
'http://example.com/book/create',
//请求体中要发送给服务端的数据
{
cat:'1',
name:'network'
},
{
'headers':{
'Content-Type':'x-www-form-urlencoded'
}
}
)
promise.then(function(response){
//成功回调
console.log(response.data)
},function(response){
//失败回调
console.log('something wrong')
})
promise.catch(function(response){
//失败回调
console.log('something wrong')
})
promise.finally(function(){
//执行完成功或者失败回调后都会执行此逻辑
})
注:所有回调函数的this都指向组件实例。
网友评论