26

Angular.js v1.0.6

Khi đưa ra một $ http.post và nhận được 200 resposne phi (401 trong trường hợp này)

$http.post('http://localhost:3030/auth/login', { 
    username: 'username', 
    password: 'password' 
}) 
.success(function(data) { 
    // Gets called on a 200 response, but not on a 401 
    console.log('success'); 
}) 
.error(function(err) { 
    // Never gets called & dies with error described below. 
    console.log('error'); 
}); 

góc ném các lỗi sau:

TypeError: Cannot read property 'data' of undefined 
    at http://localhost:9000/components/angular/angular.js:8891:22 
    at wrappedCallback (http://localhost:9000/components/angular/angular.js:6797:59) 
    at http://localhost:9000/components/angular/angular.js:6834:26 
    at Object.Scope.$eval (http://localhost:9000/components/angular/angular.js:8011:28) 
    at Object.Scope.$digest (http://localhost:9000/components/angular/angular.js:7876:25) 
    at Object.Scope.$apply (http://localhost:9000/components/angular/angular.js:8097:24) 
    at done (http://localhost:9000/components/angular/angular.js:9111:20) 
    at completeRequest (http://localhost:9000/components/angular/angular.js:9274:7) 
    at XMLHttpRequest.xhr.onreadystatechange (http://localhost:9000/components/angular/angular.js:9244:11) 

Và không bao giờ gọi là một trong hai .success() callback hoặc .error() errback làm cho nó không thể xử lý các phản ứng.

Tôi có làm gì sai không? Các cuộc gọi thành công trở lại được gọi là dự kiến ​​về cung cấp thông tin đăng nhập hợp pháp.

200 phản ứng:

Access-Control-Allow-Headers:Content-Type, Authorization, Content-Length, X-Requested-With, Auth-Token 
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS 
Access-Control-Allow-Origin:* 
Connection:keep-alive 
Content-Length:99 
Content-Type:application/json 
Date:Thu, 16 May 2013 13:57:51 GMT 

{ 
    "auth-token":"676932cc1e183a64334345944ad432d1908f8110bc", 
    "user": { 
    "id":1, 
    "username":"username" 
    } 
} 

401 phản ứng:

Access-Control-Allow-Headers:Content-Type, Authorization, Content-Length, X-Requested-With, Auth-Token 
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS 
Access-Control-Allow-Origin:* 
Connection:keep-alive 
Content-Length:45 
Content-Type:application/json 
Date:Thu, 16 May 2013 13:58:25 GMT 

{ 
    "error": [ 
    { 
     "message":"Invalid Credentials" 
    } 
    ] 
} 

Hơn nữa, nếu tôi áp dụng cú pháp hứa hẹn bình thường có lợi cho .success() phím tắt tôi nhận được một số hành vi thú vị:

$http.post('http://localhost:3030/auth/login', { 
    username: username, 
    password: password 
}).then(function (resp) { 
    // On a 200 response, resp is a response object. 
    // On a 401 response, resp is undefined. 
    console.log(resp); 
}, function() { 
    console.log('error'); 
}); 

Trả lời

39

Cuối cùng cũng đã đến cuối phần này. Vấn đề là do sự toàn cầu thực hiện HTTP đánh chặn sau:

'use strict'; 
// register the interceptor as a service 
angular.module('imvgm') 
    .factory('httpInterceptor', ['$q', '$rootScope', function($q, $rootScope) { 

    function success(response) { 
    return response; 
    } 

    function error(response) { 
    var status = response.status; 

    if (status === 401) { 
     $rootScope.$broadcast('event:loginRequired'); 
     return 
    } 
    // otherwise 
    return $q.reject(response); 
    } 

    return function(promise) { 
    return promise.then(success, error); 
    }; 

}]); 

NB

if (status === 401) { 
    $rootScope.$broadcast('event:loginRequired'); 
    return // Returns nothing 
} 

FIX:

if (status === 401) { 
    $rootScope.$broadcast('event:loginRequired'); 
} 
+2

Tôi đã có một vấn đề tương tự nhưng trong ví dụ của tôi, tôi không muốn tuyên truyền thực hiện cho các lời hứa cục bộ, đó là lý do tại sao thay vì trả về $ q.reject (response), tôi đơn giản trả về một đối tượng rỗng {}; – macpak

+0

Cảm ơn. Tôi cũng đã có vấn đề này trong thời gian dài. Cuối cùng tôi cũng đã sửa nó. –

Các vấn đề liên quan