jquery - Ajax 无法在 IOS 9.0 Cordova 中工作

标签 jquery ajax cordova

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

所以,像我说的那样改变它,错误就消失了。 enter image description here

关于jquery - Ajax 无法在 IOS 9.0 Cordova 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706656/

相关文章:

javascript - 如何在 Android 10 上使用 Cordova 创建文件夹?

javascript - Cordova 中按距离排序数组

iphone/ipad app default.png 图片问题

php - 编码行出现在每个关闭的 div 之后。 (仅在 DOM 检查器中)

ruby-on-rails - 为什么 Rails 3 中的 AJAX 如此困难?或者说,我做错了什么?

javascript - jQuery .each() 和 ajax 获取大小

php - POST 数据未出现在 CakePHP Controller 中

javascript - 当目标是可下载文件时,Ajax 函数不会在 href 上触发类

javascript - Javascript 中的动态下拉菜单与 onchange

javascript - javascript/jquery中获取运行线程id的方法