【发布时间】:2015-10-19 22:10:01
【问题描述】:
我进行的 ajax 调用在浏览器和 android 上运行良好,但是在 iphone 上运行时它会永远挂起,或者如果添加了超时选项则会超时。
ajax 看起来像:
$.ajax({
url: "http://pbcc.ca/xxx/index.php",
data: {api: 'test'},
type: "GET",
dataType: "jsonp",
jsonp: 'callback',
beforeSend: function() {
alert("beforeSend");
},
success: function(data) {
alert("success");
},
error: function(e,x) {
alert("error");
}
});
我在文档准备好和设备准备好之后尝试了它,只是在这两种情况下都在发送响应之前。网上有几篇类似问题的帖子,我尝试了一些方法,但都没有解决我的问题。任何人都可以帮忙吗?谢谢。
更新:在服务端测试,服务端没有响应来自ios的ajax调用。实际上我认为没有数据传输到服务器。
更新2:
我收到此错误:
Refused to load the script 'http://www.pbcc.ca/xxx/index.php?callback=...' because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
元:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://pbcc.ca">
Info.plist(我一开始没把它们放在一起):
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>pbcc.ca</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
config.xml(默认由cordova创建):
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.polarbear.lunch" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>xxx</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>
【问题讨论】:
-
您使用的是哪个版本的 Cordova 和 iOS?
-
cordova 5.3.3 和 ios 9.0.2(iphone 6 plus)
-
?您还尝试过哪些方法,如果您使用的是带有 Cordova 5 的 iOS 9,您可能需要在 index.html 的元标记中正确设置 Content-Security-Policy 以及为 pbcc.ca 设置 Apple 的 App Transport Security 异常您应用的 Info plist 文件。
-
@SimonPrickett Content-Security-Policy 设置是由 cordova 默认创建的。你能举一个传输安全异常设置的例子吗?我是 ios 新手,所以不知道。
-
我在网上搜索过。有人说应该设置NSAppTransportSecurity,但是我在xcode中没有找到这个key。