阿贾克斯不工作在IOS 9.0科尔多瓦 [英] Ajax Not working in IOS 9.0 Cordova

查看:181
本文介绍了阿贾克斯不工作在IOS 9.0科尔多瓦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $。阿贾克斯({
    键入:GET,
    网址:HTTP:// MyWeb即可/ PHP的,
    成功:功能(数据){
        警报(数据);
    },
    错误:函数(XHR,textStatus,ERR)
    {
        警报(readyState的:+ xhr.readyState);
        警报(responseText的:+ xhr.responseText);
        警报(状态:+ xhr.status);
        警报(文本状态:+ textStatus);
        警报(错误:+ ERR);
    }
});
 

和结果我得到的是:

 的readyState:0
responseText的:
状态:0
文本状态:错误
错误:
 

我试着在我的PHP添加标题,但仍然没有工作。之前更新我的X code到7.0和iOS模拟器9.0阿贾克斯code的工作。

 标题(内容类型:应用程序/ JSON);
标题(访问控制 - 允许 - 产地:*');
 

解决方案

据我了解整个ATS(应用传输安全性 - iOS的9 )的事情,从area28建议的方法不应该是您正在使用的应用程序内的人。

 <键> NSAppTransportSecurity< /键>
<字典>
    <键> NSAllowsArbitraryLoads< /键><真/>
< /字典>
 

这将允许所有外部请求的每个域有什么明确你不应该使用它的方式。在我看来,你应该定义一个新的<字典> 的Info.plist里面并添加此code它(编辑 info.plist中你可以使用像崇高文字等一个普通的文本编辑器。):

 <键> NSAppTransportSecurity< /键>
    <字典>
        <键> NSExceptionDomains< /键>
        <字典>
            <键> domain.tld< /键>
            <字典>
                <键> NSIncludesSubdomains< /键>
                <真/>
                <键> NSTemporaryExceptionAllowsInsecureHTTPLoads< /键>
                <真/>
                <键> NSTemporaryExceptionMinimumTLSVersion< /键>
                <字符串> TLSv1.1< /串>
            < /字典>
        < /字典>
    < /字典>
 

这将只允许请求到指定的域。所描述的方法是在WWDC 2015年其中苹果推出之一。正如你可以在截图中看到,这是苹果的方式希望用户使用它。

如果您还没有指定任何东西,你会得到

  

无法加载网页错误:资源无法加载   因为在App传输安全策略需要使用一个安全的   连接。

所以,就像我说的错误是消失改变它。

$.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.

这篇关于阿贾克斯不工作在IOS 9.0科尔多瓦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆