Troy Troy - 6 months ago 49
Android Question

Ajax requests fail after upgrading to Cordova 5.0 + cordova-android@4.0

I recently upgraded to Cordova 5.0 (and Cordova Android 4.0) and, since then, my app can no longer access external resources.

I still have

<access origin="*" />
in config.xml (as before), and I still have
<uses-permission android:name="android.permission.INTERNET" />
in AndroidManifest.xml (as before), but ajax calls are rejected with no explanation (the "textStatus" param is "error", the "errorThrown" param is null, and xhr.state() returns "rejected").

I've verified that no request is hitting the server, so it appears it is being stopped by Android, but the log doesn't give any explanation as to why...

I can access the URL in question fine from the Android browser, just not from the app.

The ajax request is made via a call to Backbone.sync() of Backbone.js, which ultimately calls jquery's $.ajax(). I haven't changed anything about how the call is made... just upgraded cordova.

Are there new requirements/setup for network requests, in Cordova 5.0, or anything I need to do differently from previous Cordova versions?

Does anyone know of a way I can get more information as to why Android and/or Cordova is rejecting the request?

Answer

I tracked the culprit down to the android@4.0.0 cordova platform. It now requires the new cordova-plugin-whitelist plugin.

It can be installed with

cordova plugin add cordova-plugin-whitelist

or by adding

<plugin name="cordova-plugin-whitelist" spec="1" />

to config.xml, and then it is configured with

<allow-navigation href="*" />

in place of the old, <access origin="*" /> tag.

It's a little annoying that the log doesn't spit out the "whitelist rejection" error messages anymore when a problem like this comes up (that would have saved me a ton a time), but maybe that'll come later.