Ionic requests return 404 only on android, in Chrome it works fine

39,372

Solution 1

The thing is that there were some major changes in Cordova 4.0.0:

Major Changes [...] - 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 I installed the Cordova Whitelist plugin. And added

<allow-navigation href="http://*/*" />

in my config.xml file.

Solution 2

In my case, the problem was with cordova-plugin-whitelist plugin. I just removed the plugin and added it. Also enabled any requests by adding this code <access origin="*" /> in config.xml. Please find below commands:

You need to remove existing plugin by using below command:

ionic cordova plugin rm cordova-plugin-whitelist

Then just add it by using below command:

ionic cordova plugin add cordova-plugin-whitelist

Hope it helps.

Solution 3

If the solutions before doesn't work on ionic 3. Thanks @rickie finally a real solution three days of maddness!!! and now is ok. Go to \platforms\android\CordovaLib\src\org\apache\cordova\engine\SystemWebViewClient.java and comment this rows:

public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // Changing this will cause problems for your application
      /*  if (!parentEngine.pluginManager.shouldAllowRequest(url)) {
            LOG.w(TAG, "URL blocked by whitelist: " + url);
            // Results in a 404.
            return new WebResourceResponse("text/plain", "UTF-8", null);
        }*/

        CordovaResourceApi resourceApi = parentEngine.resourceApi;
        Uri origUri = Uri.parse(url);
        // Allow plugins to intercept WebView requests.
        Uri remappedUri = resourceApi.remapUri(origUri);

        if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) {
            CordovaResourceApi.OpenForReadResult result = resourceApi.openForRead(remappedUri, true);
            return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream);
        }
        // If we don't need to special-case the request, let the browser load it.
        return null;
    } catch (IOException e) {
        if (!(e instanceof FileNotFoundException)) {
            LOG.e(TAG, "Error occurred while loading a file (returning a 404).", e);
        }
        // Results in a 404.
        return new WebResourceResponse("text/plain", "UTF-8", null);
    }
}
Share:
39,372

Related videos on Youtube

Victor Balan
Author by

Victor Balan

Updated on August 02, 2020

Comments

  • Victor Balan
    Victor Balan almost 4 years

    So, i have cloned the tutorial app repo from ionic. I ran

    ionic start conference sidemenu
    

    and then i added a simple $http.get('myserver')(I tried with ngResources too).

    It worked perfect on chrome, I got all the data back but on angular i only got null data and 404 status on any request I tried to do.

    Note: I tried with my hosted server and with a local one. Both fail on Android. Server is a node.js REST API.

    Nothing is printed on the console, so the request does not even get to the server.

    Has anyone experienced that or could tell me how can I debug Android apps built with Ionic?

    EDIT 1: I don`t know why do you need it but here it is

    $http.get('http://server.com/route').success(function (data) {
                //handle success
            }).error(function (data, status) {
                // handle error
            });
    
    • aorfevre
      aorfevre about 9 years
      please add some code refering your your service and how you call it with angular.
  • redrom
    redrom almost 9 years
    Thanks. I searched for this solution more than 2 days. I cannot understand, why the Cordova is not throwing exception if access origin is not defined.
  • Chris Rae
    Chris Rae almost 9 years
    I found that I also had to add a Content-Security-Policy meta tag, as per stackoverflow.com/a/30209484/498949
  • Segers-Ian
    Segers-Ian over 7 years
    I upvoted because it gave me definitely the push in the direction that I needed, the fix works but obviously still had to adjust it to not go all in with a wildcard. For security reasons obviously.
  • Develop Smith
    Develop Smith over 5 years
    This helped me. Thanks!