Cordova/Phonegap and the missing callback for 401 Unauthorized

Reading Time: 2 minutes

CordovaRecently, I’ve been working on a “hybrid mobile app” based on Cordova (f.k.a. Phonegap) technology. Simply said, I’m building a native app with screens and logic build in webtechnology (HTML(5)/CSS(3.0)/JS/etc) wrapped in a native container that communicates with native platform elements and can be deployed via the Appstores.
Although it took me some time to get the hang of the latest on hybrid app tech (one of its guts being DurandalJS as the framework for the JS integration – on which screen composition, RequireJS module integration logic and screen updating based on Knockout are depending) I am actually getting the picture and making great progress.

The more annoying it was for me to find out that I wanted to get an 401 Unauthorized for my Ajax call on the login screen (the call obviously being made to authenticate the user credentials) but I didn’t get anything back!

No callback, no error, no nothing!

I spent hours to debug this puppy and finally got to the origin of this failing expectation of mine; Google was my friend, and StackOverflow my hero: pointed out that this appeared to be a Phonegap/Cordova issue and that 401 responses were not picked up by the framework!

Then I found this Apache Cordova issue that made all clear to me:
Cordova passes the responses to the native layer and as iOS doesn’t have a response for this made there will be no action on the unauthorized answer. So that is nothing at all!

Solutions are:

  • Wait for Cordova to fix and change this behavior (although in Cordova CLI 3.3.1-0.1.2 this is still an issue)
  • Use async: false for the jQuery ajax call configuration as stated in this StackOverflow answer

Just wanted to share this with all you Cordova/Phonegap users out there so you don’t get stuck on this as long as I did. Seems there are still some hoops to go through before this thirdparty hybrid platform will cover all angles. Don’t let it stop you from building nice apps though!