Ajax不工作在IOS 9.0 Cordova

编程入门 行业动态 更新时间:2024-10-06 18:22:45
本文介绍了Ajax不工作在IOS 9.0 Cordova的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

$。ajax({ type:GET, url:http:// myweb / php, success:function (数据){ alert(data); },错误: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 文本状态:错误错误:

我尝试添加头在我的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方法不应该是您在应用程序中使用的方法。

< key> NSAppTransportSecurity< / key> < dict> < key> NSAllowsArbitraryLoads< / key>< true /> < / dict>

这将允许所有外部请求到每个域什么是最终不是你应该使用的方式。在我看来,你应该在你的 info.plist 里面定义一个新的< dict> 编辑 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>

这将只允许对您指定的域的请求。描述的方式是苹果

$.ajax({ type: "GET", url: "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

本文发布于:2023-11-26 21:47:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   Ajax   IOS   Cordova

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!