$http error function not getting called on certain scenarios (only in iOS)


#1

Hi,

When I try running the following code, the error function is invoked and the log failure1 is getting printed.

    getURL = 'http://www.google.com/testtesttest';
    $http.get(getURL).
        then(function(data) {
            console.log('success1');
        }, function(err) {
            console.log('failure1');
        });

But when I replace the URL with a another one that throws the error 401 (Unauthorized), the error function is not called. But If I fix the URL to pass the authorization, the success function is invoked and I am getting the correct data.

PS: The username and password are passed in the URL.

In all above cases, the web build (via ionic serve) and android build runs right. The issue happens only in iOS.

Update:
Tried with success/error functions, but same issue.

    $http.get(getURL).
        success(function(data, status, headers, config) {
            console.log('success');
        }). 
        error(function(data, status, headers, config) {
            console.log('failure');
        });

Update 2:
cordova-plugin-whitelist is already installed and the tag <access origin="*" /> is added in the config.xml

Update 3:

Your system information:

Cordova CLI: 5.1.1
Ionic Version: 1.0.0
Ionic CLI Version: 1.5.5
Ionic App Lib Version: 0.2.2
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Mac OS X Yosemite
Node Version: v0.12.4
Xcode version: Xcode 6.1.1 Build version 6A2008a

Any help please?


#2

It might help if you also log what the error is rather than just failure1, something like console.log('failure: ' + angular.toJson(err))'.


#3

I think I need to rephrase my statements. The problem is

  • The error function not getting called for 401 (Unauthorized) error, which happens when I pass invalid credentials.

But the error function is getting called for other errors like 404 Not Found.

and This issue happens only with iOS. In Web and Android the same code, works fine.


#4

Oh, sorry. Are you using interceptors by any chance? See https://github.com/angular/angular.js/issues/2609


#5

Hi. I don’t think I use Interpretors. Am away from System now. Anyways, thanks for the link. Will go through it :smile:


#6

Hi @encodedmirko I don’t have any custom Interceptors, checked the link, doesn’t help much.

But I will try having a basic Interceptor configured the right way and try.


#7

Hi,

Installed this Interceptor in app.config()

// Intercept http calls.
  $provide.factory('MyHttpInterceptor', function ($q) {
    return {
      // On request success
      request: function (config) {
        // console.log(config); // Contains the data about the request before it is sent.
 
        // Return the config or wrap it in a promise if blank.
        return config || $q.when(config);
      },
 
      // On request failure
      requestError: function (rejection) {
        // console.log(rejection); // Contains the data about the error on the request.
        
        // Return the promise rejection.
        return $q.reject(rejection);
      },
 
      // On response success
      response: function (response) {
        // console.log(response); // Contains the data from the response.
        
        // Return the response or promise.
        return response || $q.when(response);
      },
 
      // On response failture
      responseError: function (rejection) {
        // console.log(rejection); // Contains the data about the error.
        
        // Return the promise rejection.
        return $q.reject(rejection);
      }
    };
  });
 
  // Add the interceptor to the $httpProvider.
  $httpProvider.interceptors.push('MyHttpInterceptor');

Ref link: https://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/

It’s still the same. The code works in Web and Android with the Interceptors, but not in iOS.

Updating original post with ionic info, if that might help.


#8

Reinstalled iOS platform with following:

ionic platform rm ios
ionic platform add ios

Still the same. Can someone please help?


#9

Created a blank new tabs project and tried the same. Still not working.

Created a project is GITHUB and pushed the project there. Here is a link to the code containing the http call: https://github.com/saiy2k/Ionic-HTTP-Bug/blob/master/www/js/controllers.js


#10

Hi, i’m experiencing the same error… Is there a solution for this problem?


#11

Nothing yet. Reported a bug @ https://github.com/driftyco/ionic/issues/4031


#12

That’s a cordova ios problem. See here: