How to get status code in Angularjs?

2019-09-15 11:46发布

问题:

We use aws and swagger, also we have a service with getData method

var getData = () => {
    return $http({
        method: 'GET',
        url: 'myURL',
        headers: {
            'Authorization': 'token'
        }
    });
};

I'm trying to use it with bellow code

dataservice.getData().then(response => {
                    console.log(response)
                })
                .catch((err) => {
                    console.log(err));
                })

And for good request (when I'm autorized) it works fine, I get my data, but then token is not valid I'm gettin error whitout status code in console

Object {data: null, status: -1, config: Object, statusText: "", headers: function}  

swagger returns bellow picture

My goal is to get status code 401 'not autorized'

Our lamba has bellow structure

exports.handler =  (event, context, callback) => {
    var token = event.authorizationToken;
    // Call oauth provider, crack jwt token, etc.
    // In this example, the token is treated as the status for simplicity.

    switch (token.toLowerCase()) {
        case 'allow':
            callback(null, generatePolicy('user', 'Allow', event.methodArn));
            break;
        case 'deny':
            callback(null, generatePolicy('user', 'Deny', event.methodArn));
            break;
        case 'unauthorized':
            callback("Unauthorized");   // Return a 401 Unauthorized response
            break;
        default:
            callback("Error: Invalid token"); 
    }
};

we get this from awsdoc

回答1:

In the catch() you have access to error object that covers status and data from where you can read additional informations about reason of failure.

angular.module('app', []).service('someService', function($http) {
  const getData = () => {
    return $http({
      method: 'GET',
      url: 'myURL',
      headers: {
        'Authorization': 'token'
      }
    });
  };

  return {
    getData
  }
})

describe('Some service', function() {

  let someService, $httpBackend

  beforeEach(module('app'))

  beforeEach(inject((_someService_, _$httpBackend_) => {
    someService = _someService_
    $httpBackend = _$httpBackend_
  }))

  it('returns 401 status', () => {
    const message = {reason: 'some reason'}
  
    $httpBackend.whenGET('myURL').respond(401, { message })

    someService.getData().then((response) => {
      expect(response).toBeUndefined()
    }).catch(function(error) {
      expect(error.status).toBe(401)
      expect(error.data.message).toEqual(message)
    })

    $httpBackend.flush()
  })

})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.css"></link>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine-html.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/boot.js"></script>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="//code.angularjs.org/snapshot/angular-mocks.js"></script>



回答2:

Please try with

.success(function (response) {
 if (response.token){

 }
else{
 // do else case
}
})