【问题标题】:Phonegap Cordova Ajax requests 404 (Not Found) ErrorPhonegap Cordova Ajax 请求 404(未找到)错误
【发布时间】:2015-07-14 22:05:18
【问题描述】:

我的科尔多瓦版本是 5.0.0

当应用程序部署在设备上时,我收到所有 ajax 请求的 404 错误。在网络浏览器上,它可以正常工作,但在设备上部署相同的应用程序时无法正常工作。

我尝试添加以下内容来解决问题,但没有帮助。

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

我还在 index.html 文件中添加了以下内容,但也没有任何区别。

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

这里有人有另一个技巧来解决这个问题吗?,似乎是 phonegap 的一个很常见的问题,但上面的修复在 phonegap/cordova 的旧版本中有效,但在我的情况下没有一个有效。

任何帮助都会很棒。

【问题讨论】:

标签: jquery ajax cordova http-status-code-404


【解决方案1】:

我遇到了同样的问题,不得不安装 cordova-plugin-whitelist

cordova plugin add cordova-plugin-whitelist

感谢这篇 stackoverflow 文章 - Ajax Command to request URL no longer working

【讨论】:

  • 非常感谢。我可以确认添加这个插件使 ajax 工作。其他人来这里寻求解决方案,请确保您更新 config.xml 并添加以下内容:yourdomain.co.uk*" />
  • 我必须使用cordova plugin add https://github.com/apache/cordova-plugin-whitelist.git 来安装它。
  • 一件事:没有找到 Content-Security-Policy 元标记。使用cordova-plugin-whitelist插件时请加一个。
  • 我不敢相信当前版本的 Cordova 需要这个插件才能使 ajax 请求工作。令人失望。
  • @steve 我不认为它令人失望,但应该有更好的记录。
【解决方案2】:

它实际上应该添加cordova白名单插件:

cordova plugin add cordova-plugin-whitelist

或在您的 config.xml 文件中:

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

但如果您使用的是在线 phonegap 构建服务,则语法会有所不同。您必须在 config.xml 文件中添加以下行:

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

并授权跨域请求:

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

不建议这样做,因为在任何地方都使用通配符,并且所有内容都是允许的。但它非常适合您的测试。

【讨论】:

    【解决方案3】:

    这对我有用。唯一的区别是在我的 config.xml 中,我必须将它放在一个节点中才能生效。

    下面是我的例子:

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

    【讨论】:

    • 这是用插件吗?还是香草科尔多瓦?
    【解决方案4】:

    Phonegap 用户。将此行添加到 config.xml 是我的解决方案:

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

    【讨论】:

      【解决方案5】:

      我的问题有点不同。我使用 CI 管道编译了应用程序。所以,你仍然需要做以上所有事情(安装白名单并添加互联网权限)

      您还需要找到能够正确构建应用程序的 Visual Studio 版本。我的在构建时没有报错,但生成的应用程序无法发出任何 ajax 请求

      我降级到 VS2017 后,它可以工作了

      【讨论】:

        猜你喜欢
        • 2023-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多