Ios Cordova AJAX请求不工作
我使用的是Cordova 5.3.1,我似乎完全无法发出任何没有错误的网络请求。如果我加上Ios Cordova AJAX请求不工作,ios,ajax,cordova,Ios,Ajax,Cordova,我使用的是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
$.ajax( "http://foo.bar.com/getfeed" )
.done(function() {
alert( "success" );
})
.fail(function(jqXHR, textStatus, errorThrown) {
alert( errorThrown );
})
.always(function() {
alert( "complete" );
});
到index.js
它在iOS中失败并发出警报Error:SecurityError:DOM Exception 18
和SecurityError:未能执行“在XMLHttpRequest上打开”:拒绝连接到http://foo.bar.com/getfeed'因为它违反了文档的内容安全策略。
,所以看起来CORS被阻止了
当前,当您按照此和此安装时,无法正确安装,基本上它需要iOS平台=4.0.0-dev
。我仍然可以强制它安装旧版本,包括:
cordova plugin add cordova-plugin-whitelist@1.0.0
根据SO post和JIRA问题中的建议
但是,我仍然无法向外部域发出任何http请求。我仍然收到相同的“DOM异常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>
在index.html
中。我怀疑这不是我的模拟器阻止http请求的问题,因为这在Android和iOS模拟器以及Android设备上都是一个问题
有人知道我为什么还有CORS问题吗?谢谢。您是否尝试过将内容安全策略配置为:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
注意默认src中的*和;-在上面的示例中,我还包含了一些其他说明,以允许通用JS库工作。。。这就是我为Android和iOS构建的JQuery/Handlebars Cordova 5应用程序中使用的内容。对于iOS,如果您为iOS9构建,并且希望能够访问不安全的端点,则必须编辑plist文件。有指示 我个人在Cordova 5.3.1和Android跨域ajax请求方面仍然存在问题。这些东西在旧版本的Cordova上都很好用 [EDIT]Android需要白名单插件才能让ajax请求正常工作。Janky IMO,但很高兴ajax能在android上再次工作。什么对我有用:
<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 *">
我刚从3.3升级了cordova应用程序。?至5.3.1。在iOS请求上可以正常工作,但是它们现在需要在ios9上具有有效证书的HTTPS端点。但我在Android上也一事无成。谢谢!我已经在这件事上碰壁两天了,你刚刚解决了!我同意,这次Cordova的发布确实引起了一些问题;这意味着不需要在plist文件等中到处挖掘。谢谢!这解决了我在iOS上遇到的“Error:SecurityError:DOM Exception 18”问题。
<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 *">