admin管理员组文章数量:1200394
So I'm using angularjs restful service $resource and I'm calling $save function. However, the error callback I pass to it is not being called. The server is sending a 418 error which I thought since it's NOT 200 would result in the error callback being invoked. But, it never does. I can't find any documentation stating what http error codes will result in the error callback being called.
Here is my code:
var modalScope = $scope.$new();
modalScope.showPassword = false;
modalScope.message = null;
modalScope.user = new User();
modalScope.submit = function(user) {
user.$save( {}, function(data,headers) {
// do the success case
}, function(data,headers) {
// do the error case
});
};
The modalScope.user is being passed to the submit function defined. So what's the problem why this error callback isn't being called?
So I'm using angularjs restful service $resource and I'm calling $save function. However, the error callback I pass to it is not being called. The server is sending a 418 error which I thought since it's NOT 200 would result in the error callback being invoked. But, it never does. I can't find any documentation stating what http error codes will result in the error callback being called.
Here is my code:
var modalScope = $scope.$new();
modalScope.showPassword = false;
modalScope.message = null;
modalScope.user = new User();
modalScope.submit = function(user) {
user.$save( {}, function(data,headers) {
// do the success case
}, function(data,headers) {
// do the error case
});
};
The modalScope.user is being passed to the submit function defined. So what's the problem why this error callback isn't being called?
Share Improve this question asked Oct 26, 2012 at 4:46 chubbsondubschubbsondubs 38.7k25 gold badges108 silver badges142 bronze badges 4 |6 Answers
Reset to default 8I found the following in the ngResource source code
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with status
// code outside of the <200, 400) range
});
I am kind of confused about the range notation but it seems it should actually call the error method. Maybe you found a bug.
Had the same problem and nothing here worked. Turned out I had an custom debug-interceptor that didn't explicitly return a $q.reject(response).
Apparently every custom debug-interceptor completely overwrites the default behavior.
See https://github.com/angular/angular.js/issues/2609#issuecomment-44452795 for where I found the answer.
I had troubles with the error callback as well, but it appears that in more recent versions of AngularJS, the error callback method must now be implemented something like this:
SomeResource.query({}, angular.noop, function(response){
$scope.status = response.status;
});
Source + more detailed description: https://groups.google.com/d/msg/angular/3Q-Ip95GViI/at8cF5LsMHwJ
Also, in response to the comments on Flek's post, it seems that now only responses between 200 and 300 are not considered an error.
I couldn't get Alter's answer to work, but this worked for me:
user.$save(function (user, headers) {
// Success
console.log("$save success " + JSON.stringify(user));
}, function (error) {
// failure
console.log("$save failed " + JSON.stringify(error))
});
I'm copying from the ngResource documentation:
The action methods on the class object or instance object can be invoked with the following parameters:
- HTTP GET "class" actions: Resource.action([parameters], [success], [error])
- non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
- non-GET instance actions: instance.$action([parameters], [success], [error])
Success callback is called with (value, responseHeaders) arguments. Error callback is called with (httpResponse) argument.
$save
is considered as a non-GET "class" action, so you must to use an extra postData
parameter. Using the same question example, this should work:
modalScope.submit = function(user) {
user.$save( {}, {}, function(data,headers) {
// do the success case
}, function(response) {
// do the error case
});
};
Keep an eye on the error callback, compared with the example, is calling with just one argument, which brings all the http response.
Actually if we follow the documentation. it works
User.save(vm.user, function (response) {
FlashService.Success('Registration successful', true);
$location.path('/login');
},
function (response) {
FlashService.Error(response.data);
vm.dataLoading = false;
});
above is snip from my code it works.
本文标签: javascriptAngularJS service not invoking error callback on save() methodStack Overflow
版权声明:本文标题:javascript - AngularJS service not invoking error callback on save() method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738524210a2092339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
success
callback is always called? – Tosh Commented Oct 26, 2012 at 5:37return $q.reject(rejection);
docs.angularjs.org/api/ng/service/$http#interceptors – Dmitry Commented Sep 3, 2015 at 17:16