Jamie Hutber
Jamie Hutber

Reputation: 28076

All requests 404 with Phonegap 3.0.0

I have loaded everything required from the documentation to enable internet connectivity but all and any internet requests are received with a 404 return code.

I know that the file I am trying to reach is assessable across domain. Seen here: http://jsfiddle.net/hutber/VN864/

enter image description here

I am running this same code inside my app and on click 404 is returned in the alert.

To test my connection I am running:

    var networkState = navigator.connection.type;

    var states = {};
    if(typeof Connection!=="undefined"){
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.CELL]     = 'Cell generic connection';
        states[Connection.NONE]     = 'No network connection';

        alert('Connection type: ' + states[networkState]);

This returns the connection as expected. Currently WiFi connection

config.xml

<access origin="*" />
<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.NetworkManager" />
</feature>

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" />

JS used to call said http request

            $.ajax({
                url: 'http://stage.sexdiaries.co.uk/app/users/login',
                type: 'post',
                data: {
                    'uname': values.uname,
                    'pword': values.pword
                },
                crossDomain: true,
                error: function(data){
                    c(data.status);
                },
                success: function(data){
                    c('success: '+data);
                    data = JSON.parse(data);
                    if(data.privateKey){
                        $.jStorage.set('uid',data.ud.uid); //store user ID in the localStorage to persist
                        sessionStorage.setItem('privateKey',data.privateKey); //store privateKey in session so it disapears when the user closers the tab
                        Backbone.history.loadUrl('');
                    }
                }
            });

So I know that the connection is active. Do I really have a connection? Am I crazy? How can I reach this http request?

Added Info with iFrame

<iframe width="560" height="315" src="http://stage.sexdiaries.co.uk" frameborder="0" allowfullscreen></iframe>

This gives the following error:

enter image description here So to 100% make sure I have access to the internet I've added an iframe that should 100% work.

Upvotes: 2

Views: 1611

Answers (2)

Thomas
Thomas

Reputation: 550

I've ran into this problem as well this evening (see my comment on the original question), but I'm happy to report that I figured this out.

It turns out that it has everything to do with an update in Cordova for Android 4.0.0. From the Changlelog:

  • Whitelist functionality is now provided via plugin (CB-7747) The whitelist has been enhanced to be more secure and configurable Setting of Content-Security-Policy is now supported by the framework (see details in plugin readme) You will need to add the new cordova-plugin-whitelist plugin Legacy whitelist behaviour is still available via plugin (although not recommended).

So the way to solve this is to install the plugin:

ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git

After which you can configure which host can be navigated to, for test purposes you could set it to:

<allow-navigation href="*" />

This also explains the failing of the iframe test. I verified this fix in my project and it all just works now.

Good luck

Upvotes: 9

Hazem Hagrass
Hazem Hagrass

Reputation: 9808

I think you may forgot to allow access to your server, you may use to your config.xml file to allow access to all urls just for testing.

Upvotes: 0

Related Questions