【问题标题】:AngularJS tokenWrapper - wrong callback argumentsAngularJS tokenWrapper - 错误的回调参数
【发布时间】:2013-12-17 05:59:03
【问题描述】:

AngularJS 1.2.1
ngResource 1.2.1

我遇到了最奇怪的问题。 我正在使用 Andy Joslin (AngularJS: How to send auth token with $resource requests?) 的 tokenWrapper

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

.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 的结果将数据打印为成功,将成功打印为错误。
我该如何解决?!

【问题讨论】:

  • 你的第一行AngularJS 1.2.1 ngResource 1.1.5 激怒了我。 ngResource 也应该是 1.2.1。
  • 应该是,但是我抛出了这么多错误。我想先修复这个错误,然后更新版本

标签: javascript angularjs error-handling callback angular-resource


【解决方案1】:

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

【讨论】:

  • 我决定升级..这可能需要一些时间,但它似乎工作得更好..
  • 问题依然存在。我已经更新了角度资源,但仍然没有调用错误回调。 tokenHandler 中的某些东西必须改变我只是不知道是什么..
猜你喜欢
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多