【发布时间】: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