AngularJS 1.2.1
ngResource 1.2.1
我遇到了最奇怪的问题。我用的是安迪·乔斯林的tokenWrapper (AngularJS: How to send auth token with $resource requests?)
我有一个这样定义的资源:
.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的结果输出的数据是成功的,成功的是错误的。
我怎么才能解决这个问题!
发布于 2013-12-02 09:38:05
角1.2.0改变了承诺的处理方式。这看起来像是一个承诺问题(您没有得到suceess``failure`‘的预期顺序)。有关示例,请参见this。我猜想,坚持使用1.5中的资源是行不通的--我们将使用这种行为,因此您无法获得预期的数据。是。升级时会出现错误消息。但解决这些问题不需要太长时间。
https://stackoverflow.com/questions/20312850
复制相似问题