59

My cordova version is 5.0.0

I am getting a 404 error for the all ajax request made when the app is deployed on the device. On the web browser, it works fine but same app when deployed on a device does not work.

I tried adding following to solve the issue but it hasn't helped.

Config.xml

<access origin="*" />

AndriodManiest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

I also added following in my index.html file but it didn't make any difference either.

<script>
    $.support.cors=true;
</script>

Anyone here has another trick to solve this issue?, Seems to be a quite common issue with phonegap but the fixes above worked in older vesion of the phonegap/cordova but non of them has worked in my case.

Any help would be great.

Sahil
  • 1,959
  • 6
  • 24
  • 44

5 Answers5

118

I had the same issue and had to install the cordova-plugin-whitelist

cordova plugin add cordova-plugin-whitelist

Credit goes to this stackoverflow article - Ajax Command to request URL no longer working

Community
  • 1
  • 1
Derek Arends
  • 1,246
  • 1
  • 9
  • 3
  • 8
    Many thanks. I can confirm that adding this plugin made ajax work. Other people coming here for solution make sure you update you config.xml and add this: – Sahil May 07 '15 at 08:44
  • 2
    I had to use `cordova plugin add https://github.com/apache/cordova-plugin-whitelist.git` to install it. – Kent Robin May 21 '15 at 07:42
  • 1
    One thing: No Content-Security-Policy meta tag found. Please add one when using the cordova-plugin-whitelist plugin. – Braian Mellor Jun 15 '15 at 20:35
  • 4
    I can't believe believe the current version of Cordova _requires_ this plugin in order for ajax requests to work. Disappointing. – steve Sep 23 '15 at 22:02
  • 1
    @steve I don't think its disappointing but it should be better documented. – Mark Oct 04 '15 at 17:02
  • @MonkeyKing you think that cordova ajax requests should _not_ work out of the box? You are totally entitled to your opinions. I (personally) prefer libraries I use to work properly out of the box. – steve Oct 05 '15 at 18:38
  • i'm using cordova v5.3.3, my app was working properly on Android 2.3.3 device without any issue. But when I tried on lollipop device, ajax calls were failing with "404" error. Finally installing the plugin saved my day! Thanks a lot. – Vinay Bhargav Nov 06 '15 at 13:17
  • I was also on cordova 5.3.3 and when installing the plugin cordova says `This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.`. but the plugin solved the problem on android 6.0.1 where POST kept returning 404. – pellekrogholt Mar 04 '16 at 09:59
  • I have installed the plugin and also created the proper config in `config.xml` and added some Content Policy Rules. I still get a 404. The request never reaches my backend so it must be blocked by the app. – Phillipp Apr 03 '19 at 09:18
  • Thanks to this answer I manage it fixed the issue everything I throw at the ajax always returns 404 even google :D – Christopher Pelayo Sep 22 '19 at 07:53
23

It should actually add the cordova whitelist plugin :

cordova plugin add cordova-plugin-whitelist

or in your config.xml file :

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

but if you are using the online phonegap build service the syntax is different. You have to add the following line in your config.xml file :

<gap:plugin name="cordova-plugin-whitelist" source="npm" />

and authorize cross domain requests :

<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

This is not recommended because a wildcard is used everywhere and everything is permitted. But it is perfect for your tests.

Damien
  • 3,915
  • 1
  • 17
  • 18
1

This worked for me. The only difference is in my config.xml I had to put it in an node for it to take effect.

My example below:

<platform name="android">
    <allow-intent href="market:*" />
    <access origin="*" />
    <allow-intent href="*" />
    <allow-navigation href="*" />
</platform>
0

Phonegap User. Adding this line into the config.xml is the solution for me:

<gap:plugin name="cordova-plugin-whitelist" source="npm" />
0

My problem was a bit different. I compiled the app using a CI pipeline. So, you still need to do all the above (install the whitelist and add internet permission)

and you also need to find a Visual Studio version that will build the app correctly. Mine doesn't give an error when building, but the resulting app can't make any ajax request

After I downgraded to VS2017, it works

grandia
  • 709
  • 7
  • 20