Search code examples
angularjsionic-frameworkngcordova

All $http requests are returning -1


I am working on an app using ionic. Just today, all of my $http requests started failing by returning -1 status codes. This is happening when I run with ionic serve, ionic run browser and ionic emulate ios.

These $http requests are not necessarily remote, either. It's failing to load the HTML files inside of www/template. Any help with debugging would be greatly appreciated!

Some more details: When running ionic serve, it loads index.html just fine, which loads my app.js. That in turn sets up an HTTP interceptor and loads the states using $stateProvider:

angular.module('starter', ['ionic', 'ngCordova', 'ionic.service.core', 'starter.controllers', 'setup.controllers', 'settings.controllers', 'history.controllers', 'graph.controller'])
...
    .config(function ($stateProvider, $urlRouterProvider, $httpProvider, $analyticsProvider) {
        ...
        $httpProvider.interceptors.push(['$q', '$location', function ($q, $location) {
            return {
                'request': function (config) {
                    // Setup authorization token...
                    return config;
                },  
                'responseError': function (response) {
                    if (response.status === 401 || response.status === 403) {
                        $location.path('/login');
                    }   
                    return $q.reject(response);
                }   
            };  
        }]);
        ...
            $stateProvider.state('login', {
            cache: false,
            url: '/login',
            templateUrl: 'templates/login.html',
            controller: 'LoginCtrl'
        })  

            // setup an abstract state for the tabs directive
            .state('tab', {
                url: '/tab',
                abstract: true,
                cache: false,
                controller: 'TabCtrl',
                templateUrl: 'templates/tabs.html'
            })
...

My browser's javascript console is filled with these errors:

ionic.bundle.js:25000 GET http://localhost:8100/templates/tabs.html net::ERR_EMPTY_RESPONSE(anonymous function) @ ionic.bundle.js:25000sendReq @ ionic.bundle.js:24793serverRequest @ ionic.bundle.js:24503processQueue @ ionic.bundle.js:29127(anonymous function) @ ionic.bundle.js:29143$eval @ ionic.bundle.js:30395$digest @ ionic.bundle.js:30211$apply @ ionic.bundle.js:30503(anonymous function) @ ionic.bundle.js:32332completeOutstandingRequest @ ionic.bundle.js:19194(anonymous function) @ ionic.bundle.js:19470
ionic.bundle.js:25000 GET http://localhost:8100/templates/login.html net::ERR_EMPTY_RESPONSE(anonymous function) @ ionic.bundle.js:25000sendReq @ ionic.bundle.js:24793serverRequest @ ionic.bundle.js:24503processQueue @ ionic.bundle.js:29127(anonymous function) @ ionic.bundle.js:29143$eval @ ionic.bundle.js:30395$digest @ ionic.bundle.js:30211$apply @ ionic.bundle.js:30503(anonymous function) @ ionic.bundle.js:32332completeOutstandingRequest @ ionic.bundle.js:19194(anonymous function) @ ionic.bundle.js:19470

When I drop a breakpoint in the interceptor, it intercepts the requests for these local HTML files and shows the response status code as -1.

UPDATE 1: It keeps getting stranger... if I clear out the local storage, it works... once. I use local storage to store the logged in user account, so when I clear the storage and refresh, it successfully loads the pages. After I log in, all the requests stop working.


Solution

  • I spent a lot of time during investigation this issue and finally find out the problem. In my case I have interceptor which adds Bearer Authorization data to each request, all worked amazing until this Bearer had small size, however I made design mistake and added a large amount of data to it after that problems started. After minimizing Bearer request size all work as expected.