简体   繁体   中英

Error handling on $http.get

I have a GET request using the $http service. However, the error handling is not executed.

This is the error I am getting, where I expect the console to print "You are not logged in" . (The API will respond with a 401 if the check fails)

错误信息

Here's the Angular code

$http.get($rootScope.api + '/authentication/check-login').then(function(response) {
    console.log('You are logged in');
}, function(response) {
    console.log('You are not logged in');
});

What am I doing wrong?

Use .error .

$http.get($rootScope.api + '/authentication/check-login')
.success(function(response) {
    console.log('You are logged in');
})
.error(function(response) {
    console.log('You are not logged in');
});

Try this--

$http.get($rootScope.api + '/authentication/check-login').success(function(data) {
        console.log('You are logged in');
      }).
      error(function(response) {
        console.log('You are not logged in');
      });

Source doc for more info- https://docs.angularjs.org/api/ng/service/ $http

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM