Ionic 仅在 android 上请求 return 404,在 Chrome 中工作正常

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

所以,我已经从 ionic 克隆了教程应用程序库。我运行

ionic start conference sidemenu

然后我添加了一个简单的 $http.get('myserver')(我也尝试过使用 ngResources)。

它在 chrome 上运行完美,我得到了所有数据,但在 angular 上,我尝试执行的任何请求都只得到空数据和 404 状态。

注意:我尝试使用我的托管服务器和本地服务器。两者都在 Android 上失败了。 服务器是 node.js REST API.

控制台上没有打印任何内容,因此请求甚至没有到达服务器。

有没有人经历过或者可以告诉我如何调试使用 Ionic 构建的 Android 应用程序?

编辑 1:我不知道你为什么需要它,但它就在这里

$http.get('http://server.com/route').success(function (data) {
            //handle success
        }).error(function (data, status) {
            // handle error
        });

事实是 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).

所以我安装了 Cordova Whitelist plugin。并添加

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

在我的 config.xml 文件中。

你应该的本地内容,当 cordova compile 是一个 www 文件夹时,它们有资产和其他文件夹供你实现 apk 或等效 iOS

<img src="assets/images/{your-file-name}">

就我而言,问题出在 cordova-plugin-whitelist 插件上。我刚刚删除了插件并添加了它。还通过添加此代码启用任何请求 <access origin="*" />config.xml 中。请找到以下命令:

您需要使用以下命令删除现有插件:

ionic cordova plugin rm cordova-plugin-whitelist

然后使用以下命令添加它:

ionic cordova plugin add cordova-plugin-whitelist

希望对您有所帮助。

如果之前的解决方案对 ionic 3 不起作用。 感谢@rickie 终于找到了一个真正的解决方案,疯狂了三天!!!现在可以了。转到 \platforms\android\CordovaLib\src\org\apache\cordova\engine\SystemWebViewClient.java 并评论此行:

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);
    }
}