【问题标题】:Cordova AJAX request not workingCordova AJAX 请求不起作用
【发布时间】:2015-12-21 00:32:26
【问题描述】:

我使用的是 Cordova 5.3.1,我似乎完全无法毫无错误地发出任何网络请求。如果我添加

$.ajax( "http://foo.bar.com/getfeed" )
.done(function() {
    alert( "success" );
})
.fail(function(jqXHR, textStatus, errorThrown) {
    alert( errorThrown );
})
.always(function() {
    alert( "complete" );
});

index.js 它失败并在iOS 和SecurityError: Failed to execute 'open on 'XMLHttpRequest': Refused to connect to 'http://foo.bar.com/getfeed' because it violates the document's Content Security Policy. 中警告Error: SecurityError: DOM Exception 18,所以看起来CORS 被阻止了。

目前whitelist plugin按照SO postJIRA issue安装时无法正确安装,基本上它需要iOS平台>=4.0.0-dev。我仍然可以强制它安装旧版本:

cordova plugin add cordova-plugin-whitelist@1.0.0

根据 SO 帖子和 JIRA 问题中的建议。

但是我仍然无法向外部域发出任何 http 请求。我仍然收到相同的“DOM Exception 18”错误。我的config.xml 文件目前是

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.company.pearstest" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>CORS test</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" version="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

最近,允许不同来源的标签似乎在不同的版本上发生了变化,因此我尝试了这个文件中各种不同标签的每一种可能的组合。

我还检查了$.support.cors 以确保 jQuery 允许 CORS 并且它确实允许(jQuery 正常工作,因为 AJAX 请求正在运行失败回调而不是静默失败)。

我也有元标记

<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">

index.html。我怀疑这不是我的模拟器阻止 http 请求的问题,因为它在 Android 和 iOS 模拟器以及 Android 设备上都是一个问题。

有人知道为什么我仍然遇到 CORS 问题吗?谢谢。

【问题讨论】:

  • 我刚刚从 3.3 升级了我的 cordova 应用程序。?至 5.3.1。在 iOS 请求上工​​作正常,但是他们现在需要在 ios9 上具有有效证书的 HTTPS 端点。但我也无法使用 Android。

标签: ios ajax cordova


【解决方案1】:

您是否尝试过将 Content-Security-Policy 配置为如下所示:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

注意 default-src 中的 * 和 ; 的使用- 我还在上面的示例中包含了一些其他说明,以允许常见的 JS 库工作......这是我在为 Android 和 iOS 构建的 JQuery / Handlebars Cordova 5 应用程序中使用的。

【讨论】:

  • 谢谢!这解决了我在 iOS 上的“错误:SecurityError:DOM Exception 18”的问题。
【解决方案2】:

对于 iOS,如果您正在为 iOS9 构建并且希望能够访问不安全的端点,则必须编辑您的 plist 文件。 This gist有说明。

我个人仍然对 Cordova 5.3.1 和 Android 跨域 ajax 请求有疑问。这些东西在旧版本的 Cordova 上都可以正常工作。

[编辑] Android 需要白名单插件才能使 ajax 请求工作。 Janky IMO,但很高兴 ajax 再次在 android 上运行。

【讨论】:

  • 谢谢!我已经在墙上撞了两天了,你刚刚解决了它!我同意,这个 Cordova 版本确实引起了一些问题;这是为了避免在 plist 文件等中挖掘的需要。
【解决方案3】:

什么对我有用:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src *">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2014-12-28
    • 2017-02-11
    • 2016-05-08
    • 2015-09-08
    相关资源
    最近更新 更多