Angular jsonp return status code 0 instead of 401

I have an angular app that makes a call to a resting endpoint, which is safe and returns 401 on access. I defined an interceptor in my application so that the errors were caught, and after 401 the application should redirect to the login page. It sounds very simple, but when I launch it in a browser, I get a status code 0 in my error response. However, in my Chrome console, I see a log saying that the requested URL resulted in 401.

I need to add that the JS files are located on localhost: 9000, and the application that gives the answer 401 is on localhost: 8080. What is wrong?

    angular.module('ndbAdminUIApp', ['ngResource']).config(['$routeProvider', '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
        $routeProvider.when('/', {
            templateUrl : 'views/main.html',
            controller : 'MainCtrl'
        }).when('/login', {
            templateUrl : 'views/login.html',
            controller : 'LoginCtrl'
        }).otherwise({
            redirectTo : '/'
        });

        var interceptor = ['$location', '$q', function ($location, $q) {
            function success(response) {
                return response;
              }

            function error(response) {
                if (response.status === 401) {
                  $location.path('#login');
                  return $q.reject(response);
                }
                else {
                  return $q.reject(response);
                }
              }

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

      $httpProvider.responseInterceptors.push(interceptor);
    }])


    angular.module('ndbAdminUIApp').factory('ndbSystemService', function($http) {
        return {
            heartBeat: function() {
                $http.jsonp('http://localhost\:8080/ndb-service/r/system/hb?callback=JSON_CALLBACK');
              },
          };
      });
+4
source share
2 answers

, Angular JSONP Angular, , , . , , - - .

0

- angular.callbacks._0 ({jsonResponseData}). jsonResponseData ( ) . :

var stringForResponse = new StringBuilder();
                stringForResponse.Append(callbackFunctionName);
                stringForResponse.Append("(");
                stringForResponse.Append(jsonResponseData);
                stringForResponse.Append(")");
                toString = stringForResponse.ToString();
0

Source: https://habr.com/ru/post/1502715/


All Articles