我已经处理这个问题好几个小时了,我无法弄清楚为什么当我的 Rails 后端引发正确的错误时,angular 没有触发我的错误回调。我使用的是 angular 1.2.0rc1。
根据文档:
非 GET“类”操作:Resource.action([parameters], postData, [success], [error])
我在保存产品操作期间在我的 Angular Controller 中使用它:
$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 Controller :
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
Rails 在尝试保存具有重复 sku 的产品时返回 422 状态,我想在前端显示错误消息。
我希望 Angular 应该执行更新调用中提供的错误处理函数,但我做不到那么远。相反,在我的控制台中我看到:
TypeError:无法使用无用的堆栈跟踪读取未定义的属性“数据”
:
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 时将用户重定向到注册页面。它一定是干扰,但我不确定我应该如何修复
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');
}
});
};
}]);
最佳答案
您还需要拒绝拦截器中的 promise ,否则它会被视为您已经“处理”了异常。
所以:
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);
});
};
}]);
关于ruby-on-rails - Angular 资源不调用我的错误回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18907117/