Ajax 在 IOS 9.0 Cordova 中不起作用 [英] Ajax Not working in IOS 9.0 Cordova
问题描述
$.ajax({类型:获取",url: "http://myweb/php",成功:功能(数据){警报(数据);},错误:函数(xhr,文本状态,错误){alert("readyState:" + xhr.readyState);alert("responseText:"+ xhr.responseText);警报(状态:" + xhr.status);alert("文本状态:" + textStatus);警报(错误:" + 错误);}});
我得到的结果是:
readyState:0响应文本:""状态:0文本状态:错误错误:""
我尝试在我的 php 中添加标题,但仍然无法正常工作.ajax 代码在我将 xcode 更新到 7.0 和 ios 模拟器到 9.0 之前工作.
header('Content-Type: application/json');header('Access-Control-Allow-Origin: *');
据我了解整个 ATS (App Transport Security - iOS 9) 的事情,area28 推荐的方法不应该是您在应用程序中使用的那个.
NSAppTransportSecurity <字典><key>NSAllowsArbitraryLoads</key><true/></dict>
这将允许对每个域的所有外部请求,这绝对不是您应该使用的方式.在我看来,您应该在 info.plist
中定义一个新的
并将此代码添加到其中(以编辑 info.plist
> 你可以使用普通的文本编辑器,比如 sublime text 等):
NSAppTransportSecurity <字典><key>NSExceptionDomains</key><字典><key>domain.tld</key><字典><key>NSIncludesSubdomains</key><真/><key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key><真/><key>NSTemporaryExceptionMinimumTLSVersion</key><string>TLSv1.1</string></dict></dict></dict>
这将只允许向您指定的域发出请求.所描述的方式是苹果
$.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);
}
});
And the result I get is:
readyState:0
responseText:""
status:0
text status:error
error:""
I try add header in my php, but still not working. The ajax code work before i update my xcode to 7.0 and ios simulator to 9.0.
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
As far as i understood the whole ATS (App Transport Security - iOS 9) thing, the recommended method from area28 should not be the one you're using inside an application.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
This will allow all external requests to every domain what is definitively not the way you should use it. In my opinion you should define a new <dict>
inside your info.plist
and add this code to it (to edit the info.plist
you can just use a normal text editor like sublime text etc.):
<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>
This will only allow requests to the domain you specified. The described way is the one which apple introduced on the WWDC 2015. As you can see on the screenshot, it's the way apple want the users to use it.
If you haven't specified anything, you'll get
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.
So, change it like i said and the error is gone away.
这篇关于Ajax 在 IOS 9.0 Cordova 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!