![](/img/trans.png)
[英]Issue with passing arguments to a named callback function in angularjs/javascript
[英]AngularJS tokenWrapper - wrong callback arguments
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和token包裝,隨每個請求一起發送,這很棒。 問題在於調用錯誤回調。
當使用這樣的資源時
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以上的資源不起作用,我們會采用這種行為,因此您沒有得到預期的數據,是的,升級時會出現錯誤消息,但是解決這些問題不會花費很長時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.