【问题标题】:Ionic requests return 404 only on android, in Chrome it works fine离子请求仅在 android 上返回 404,在 Chrome 中它工作正常
【发布时间】:2015-07-01 20:19:20
【问题描述】:

所以,我已经从 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
        });

【问题讨论】:

  • 请添加一些代码来引用您的服务以及如何使用 Angular 调用它。

标签: android angularjs http ionic-framework ionic


【解决方案1】:

问题是 Cordova 4.0.0 发生了一些重大变化:

主要变化 [...] - 现在通过插件 (CB-7747) 提供白名单功能 白名单已得到增强,更加安全和可配置 框架现在支持 Content-Security-Policy 的设置 (请参阅插件自述文件中的详细信息)您需要添加新的 cordova-plugin-whitelist plugin 旧的白名单行为仍然是 可通过插件获得(虽然不推荐)。

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

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

在我的config.xml 文件中。

【讨论】:

  • 谢谢。我搜索了这个解决方案超过 2 天。我不明白,如果没有定义访问源,为什么 Cordova 不会抛出异常。
  • 我发现我还必须添加一个 Content-Security-Policy 元标记,按照stackoverflow.com/a/30209484/498949
  • 我投了赞成票,因为它确实给了我在我需要的方向上的推动力,修复工作有效,但显然仍然需要调整它以不使用通配符。显然是出于安全原因。
【解决方案2】:

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

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

ionic cordova plugin rm cordova-plugin-whitelist

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

ionic cordova plugin add cordova-plugin-whitelist

希望对你有帮助。

【讨论】:

  • 这对我有帮助。谢谢!
【解决方案3】:

如果之前的解决方案不适用于 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);
    }
}

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2016-04-17
      • 2021-05-09
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多