javascript - Unable to catch 401 status code with Angular HttpInterceptor -
i have created http response interceptor checks 401 status code. if code 401, supposed write in browser console, not work. doesn't seem catch it.
app.js:
'use strict'; // declare app level module depends on views, , components var app = angular.module('myapp', []); app.factory('authhttpresponseinterceptor', ['$q', '$location', function ($q, $location) { return { response: function (response) { if (response.status === 401) { console.log("response 401"); } else { console.log("other response no 401"); } return response || $q.when(response); }, responseerror: function (rejection) { if (rejection.status === 401) { console.log("response error 401", rejection); } return $q.reject(rejection); } } }]) .config(['$httpprovider', function ($httpprovider) { //http intercpetor check auth failures xhr requests $httpprovider.interceptors.push('authhttpresponseinterceptor'); }]); index.html
<html lang="en" ng-app="myapp"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>my angularjs app</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div ng-controller="testctrl"></div> <script src="bower_components/angular/angular.js"></script> <script src="app.js"></script> <script src="testcontroller.js" type="text/javascript"></script> </body> </html> testcontroller.js
app.controller('testctrl', function ($scope, $http, $timeout) { $http.get('http://ec2-54-229-72-240.eu-west-1.compute.amazonaws.com:8080/webservice/api/stuffs/onestuff', { headers: {'authorization': 'bearer ' + localstorage.getitem('access_token')} }).success(function (data) { alert(data.id); }); }); the access token stored in local storage previous landing/login page. i'm trying achieve redirect user landing page if token isnt valid anymore, or if doesn't have one.
with valid token, no problem, retrieve data api. when manually change stored token, in order trigger 401, nothing written in browser console.
i'm sure it's 401 unauthorized reponse, because checked in chrome network observer. here's thing :
xmlhttprequest cannot load http://ec2-54-229-72-240.eu-west-1.compute.amazonaws.com:8080/webservice/api/students/chat. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:8383' therefore not allowed access. response had http status code 401. and here requests ( yes, because sometime have 1 request in list, , when refresh page sometime append there 2 of them )
name status type initiator stuff 200 xhr angular.js:10765 stuff 401 xhr other here's talking about, time, when refreshed page had 2 requests done, it's strange because 1 request in code.
and sometime, after few refresh have :
name status type initiator stuff 401 xhr other but in both cases, never had written in console, proves me 401 error has been caught.
thank you, , sorry bad english !
try add request , requesterror functions (just in case, know optional):
request: function(config) { return config; }, requesterror: function(err) { $q.reject(err); }, http://jsfiddle.net/tjruzpmb/218/
i changed url works. please notice comments. error 401 never catched in response() because interceptor redirects 401 responseerror().
Comments
Post a Comment