我已经在这几个小时了,我无法弄清楚为什么当我的rails后端引发一个正确的错误时,angular不会触发我的错误回调.我正在使用角度1.2.0rc1. 根据文件: 非GET“类”操作:Resource.action([param
根据文件:
非GET“类”操作:Resource.action([parameters],postData,[success],[error])
我在保存产品操作期间在角度控制器中使用它:
$scope.saveProduct = function(product){
if (product.id) {
Product.update({id: product.id},{product: product}, function(data){
console.log('handle success')
}, function(){
console.log('handle error') //THIS IS NEVER OUTPUT!
});
}
}
这是资源定义:
angular.module('sellbriteApp.resources').factory('Product', function ($resource) {
return $resource('/api/products/:id', { id: "@id" },
{
'create': { method: 'POST' },
'index': { method: 'GET', isArray: true },
'show': { method: 'GET', isArray: false },
'update': { method: 'PUT' },
'destroy': { method: 'DELETE' }
}
);
});
这是我的rails控制器:
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to [:edit, @product], notice: 'Product was successfully updated.' }
format.json { render 'products/show.json.jbuilder', status: :accepted }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
尝试保存具有重复sku的产品时,Rails返回422状态,并且我想在前端显示错误消息.
我希望angular应该执行更新调用中提供的错误处理函数,但我无法做到这一点.而是在我的控制台中,我看到:
TypeError:无法使用无用的堆栈跟踪读取未定义的属性“data”:
TypeError: Cannot read property 'data' of undefined at $http.then.value.$resolved (http://localhost:9000/bower_components/angular-resource/angular-resource.js:477:32) at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:9042:59) at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:9042:59) at http://localhost:9000/bower_components/angular/angular.js:9128:26 at Object.Scope.$eval (http://localhost:9000/bower_components/angular/angular.js:9953:28) at Object.Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:9809:23) at Object.$delegate.__proto__.$digest (<anonymous>:844:31) at Object.Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:10039:24) at Object.$delegate.__proto__.$apply (<anonymous>:855:30) at done (http://localhost:9000/bower_components/angular/angular.js:6542:45)
我错过了什么?
更新:
显然这个http拦截器是相关的.如果我将此代码注释掉,则会调用错误函数.我已经从其他地方复制了这个片段并对其进行了修改,以便在用户未登录时点击rails api将用户重定向到sign_up页面.它必须是干扰,但我不知道应该如何解决它.
App.config(['$httpProvider', function ($httpProvider) {
$httpProvider.responseInterceptors.push('securityInterceptor');
}]);
App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', function ($injector,$location,$cookieStore) {
return function(promise) {
var $http = $injector.get('$http');
return promise.then(null, function(response){
if (response.status === 401) {
$cookieStore.remove('_angular_devise_merchant');
toastr.warning('You are logged out');
$location.path('/sign_in');
}
});
};
}]);
你需要在拦截器中拒绝承诺,否则它被视为你已“处理”异常.
所以:
App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', '$q', function ($injector,$location,$cookieStore, $q) {
return function(promise) {
var $http = $injector.get('$http');
return promise.then(null, function(response){
if (response.status === 401) {
$cookieStore.remove('_angular_devise_merchant');
toastr.warning('You are logged out');
$location.path('/sign_in');
}
return $q.reject(response);
});
};
}]);
