AngularJS tokenWrapper-错误的回调参数

AngularJS tokenWrapper - wrong callback arguments

本文关键字:回调 参数 错误 tokenWrapper- AngularJS      更新时间:2024-01-16

AngularJS 1.2.1
ngResource 1.2.1

我遇到了最奇怪的问题。我正在使用Andy Joslin的tokenWrapper(AngularJS:如何使用$resource请求发送身份验证令牌?)

我有一个这样定义的资源:

.factory('someService', ['$resource', 'api_host', 'TokenHandler',
  function($resource, api_host, TokenHandler) {
    var Resource  = $resource(api_host + 'applicant/:command/:xxx', { xxx: '@xxx', command: '@command' }, {
        'get': { method: 'GET', isArray: false },
        'save': { method: 'POST', isArray: false },
        'create': { method: 'put', isArray: false },
        'message': { method: 'post', isArray: false }
    });
    Resource  = TokenHandler.wrapActions( Resource,
      ["query", "get", "save", "remove", "create", "message"] );
    return Resource;
}])

它由tokenHandler包装,令牌随每个请求一起发送,这太棒了。问题在于调用错误回调。

当使用类似的资源时

var interview = new someService({ command: 'doSomething', xxx: $scope.xxx});
    interview.$create({}, function(response) {
      console.log(response);
    }, function(e) {
      alert('error');
    });

问题
当资源返回200时,成功函数(第一个参数)将被调用。当资源返回其他内容时,不会调用错误回调。

我已经尝试记录tokenHandler,它似乎正在聚集参数。

    var tokenWrapper = function( resource, action ) {
  // copy original action
  resource['_' + action]  = resource[action];
  // create new action wrapping the original and sending token
  resource[action] = function( data, success, error){
    console.log(success, 'success');
    console.log(error, 'error');
    return resource['_' + action](
      angular.extend({}, data || {}, {token: tokenHandler.get()}),
      success,
      error
    );
  };
};

console.log的结果将数据打印为成功,将成功打印为错误。
我该怎么修?!

angular 1.2.0改变了承诺的处理方式。这看起来像是一个承诺问题(你没有得到suceess``失败的预期顺序```。请看这个例子。我猜坚持使用1.5中的资源是行不通的,我们会处理这种行为,所以你没有得到预期的数据。是的。升级时会有错误消息。但解决它们应该不会花很长时间。