$.ajax({
type: "GET",
url: "http://myweb/php",
success: function (data){
alert(data);
},
error:function(xhr,textStatus,err)
{
alert("readyState: " + xhr.readyState);
alert("responseText: "+ xhr.responseText);
alert("status: " + xhr.status);
alert("text status: " + textStatus);
alert("error: " + err);
}
});
我得到的结果是:
readyState:0
responseText:""
status:0
text status:error
error:""
我尝试在我的 php 中添加 header ,但仍然不起作用。在我将 xcode 更新到 7.0 并将 ios 模拟器更新到 9.0 之前,ajax 代码可以工作。
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
最佳答案
据我了解整个 ATS(应用程序传输安全 - iOS 9)的情况,area28 推荐的方法不应该是您在应用程序中使用的方法。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
这将允许对每个域的所有外部请求,但这绝对不是您应该使用的方式。在我看来,你应该定义一个新的 <dict>
在你的info.plist
里面并将此代码添加到其中(要编辑info.plist
,您可以使用普通的文本编辑器,如sublime text等):
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>domain.tld</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
这将只允许对您指定的域的请求。所描述的方式是苹果 introduced on the WWDC 2015 的方式。正如您在屏幕截图中看到的,这就是苹果希望用户使用它的方式。
如果您没有指定任何内容,您将得到
Failed to load webpage with error: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
关于jquery - Ajax 无法在 IOS 9.0 Cordova 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706656/