programing

Angular.js $http.Post TypeError: 정의되지 않은 속성 'data'를 읽을 수 없습니다.

linuxpc 2023. 2. 25. 19:51
반응형

Angular.js $http.Post TypeError: 정의되지 않은 속성 'data'를 읽을 수 없습니다.

Angular.js v1.0.6

$http를 만들 때.200 이외의 응답(이 경우는 401)을 투고해 수신한다.

$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');
});

Angular는 다음 오류를 발생시킵니다.

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) 

그리고 절대 그 둘 다라고 부르지 않는다..success()콜백 또는.error()에러백으로 인해 응답을 처리할 수 없습니다.

내가 뭘 잘못하고 있나요?정상적인 credential이 제공되면 성공 콜백이 예상대로 호출됩니다.

200 응답:

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 응답:

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"
    }
  ]
}

또한 .success() 단축키를 위해 일반 약속 구문을 채택하면 다음과 같은 흥미로운 동작을 얻을 수 있습니다.

$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');
});

마침내 이 사건의 진상을 밝혀냈어.이 문제는 다음 글로벌HTTP 대행 수신기의 실장에 의한 것입니다.

'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);
  };

}]);

N.B.

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

수정:

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

언급URL : https://stackoverflow.com/questions/16589752/angular-js-http-post-typeerror-cannot-read-property-data-of-undefined

반응형