【发布时间】: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 post和JIRA 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。