javascript AngularJS 服务未在 save() 方法上调用错误回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13080877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:46:49  来源:igfitidea点击:

AngularJS service not invoking error callback on save() method

javascriptrestangularjs

提问by chubbsondubs

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.

所以我正在使用 angularjs restful 服务 $resource 并且我正在调用 $save 函数。但是,我传递给它的错误回调没有被调用。服务器正在发送 418 错误,我认为因为它不是 200 会导致调用错误回调。但是,它永远不会。我找不到任何说明哪些 http 错误代码将导致错误回调被调用的文档。

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?

modalScope.user 被传递给定义的提交函数。那么为什么不调用此错误回调的问题是什么?

采纳答案by F Lekschas

I found the following in the ngResource source code

我在 ngResource 源代码中找到了以下内容

$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.

我对范围表示法有点困惑,但似乎它实际上应该调用错误方法。也许你发现了一个错误。

回答by Ruud van der Linden

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).

有同样的问题,这里没有任何工作。结果我有一个自定义的调试拦截器,它没有明确返回 $q.reject(response)。

Apparently every custom debug-interceptor completely overwrites the default behavior.

显然,每个自定义调试拦截器都会完全覆盖默认行为。

See https://github.com/angular/angular.js/issues/2609#issuecomment-44452795for where I found the answer.

请参阅https://github.com/angular/angular.js/issues/2609#issuecomment-44452795了解我在哪里找到的答案。

回答by sgdesmet

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:

我也遇到了错误回调的问题,但似乎在更新的 AngularJS 版本中,错误回调方法现在必须实现如下:

SomeResource.query({}, angular.noop, function(response){
  $scope.status = response.status; 
});

Source + more detailed description: https://groups.google.com/d/msg/angular/3Q-Ip95GViI/at8cF5LsMHwJ

来源+更详细的描述: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.

此外,针对 Flek 帖子的评论,现在似乎只有 200 到 300 之间的回复不被视为错误。

回答by mstreffo

I couldn't get Alter's answer to work, but this worked for me:

我无法得到 Alter 的工作答案,但这对我有用:

user.$save(function (user, headers) {
                    // Success
                    console.log("$save success " + JSON.stringify(user));
                }, function (error) {
                    // failure
                    console.log("$save failed " + JSON.stringify(error))
                });

回答by Alter Lagos

I'm copying from the ngResource documentation:

我正在从ngResource 文档中复制:

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.

可以使用以下参数调用类对象或实例对象上的操作方法:

  • HTTP GET“类”操作:Resource.action([parameters], [success], [error])
  • 非 GET“类”操作:Resource.action([parameters], postData, [success], [error])
  • 非 GET 实例操作: instance.$action([parameters], [success], [error])

使用 (value, responseHeaders) 参数调用成功回调。使用 (httpResponse) 参数调用错误回调。

$saveis considered as a non-GET "class" action, so you must to use an extra postDataparameter. Using the same question example, this should work:

$save被视为非 GET “类”操作,因此您必须使用额外的postData参数。使用相同的问题示例,这应该有效:

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.

注意错误回调,与示例相比,它只使用一个参数调用,它带来了所有的 http 响应。

回答by Rajender Saini

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.

上面是从我的代码中剪下来的。