2015-09-22 53 views
6
$.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); 
    } 
}); 

工作,結果我得到的是:阿賈克斯不能在IOS 9.0科爾多瓦

readyState:0 
responseText:"" 
status:0 
text status:error 
error:"" 

我試着在我的PHP添加標題,但仍然沒有工作。在我更新我的xcode到7.0和ios模擬器到9.0之前,ajax代碼工作。

header('Content-Type: application/json'); 
header('Access-Control-Allow-Origin: *'); 

回答

12

至於我全明白了ATS(應用交通運輸安全 - 的iOS 9)的事情,從area28推薦的方法不應該是你使用的是應用程序內的一個。

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

這將允許所有外部請求的每個域有什麼明確不應該使用它的方式。在我看來,你應該定義你的info.plist內新<dict>這個代碼添加到它(編輯info.plist你可以只使用一個普通的文本編輯器,如崇高文字等):

<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

+0

我同意。兩者都可以工作,如果需要,您可以獲得更具體的信息。 – area28

+1

不是你**可以**,你**應該**更具體!這些是我認爲無法討論的安全主題。 – Sithys

+0

將每個域名列入白名單的第二種方法絕對是首選方式,第一種方式在iOS9之前的iOS中不起作用。(在iOS 7上測試) – maechler

5

如果您在Xcode項目的工作,你可能需要編輯info.plist文件。安全性隨iOS9更改。如有可能,Apple強烈建議使用https進行網絡請求。 Here是與這個問題有關的答案。

從那篇文章:

添加到您的info.plist文件。

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

App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app's Info.plist file.

+0

感謝它,它的工作〜 – JohnLoong

+1

這對我有效。將設備更新到iOS9後,我的應用程序無法加載,現在它像魅力一樣工作。拯救生命,謝謝! –

1

對我來說,這是@ area28's和@Sithys的答案的組合。所以我最終把這個添加到我的info.plist中:

<dict> 
<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
    <key>NSExceptionDomains</key> 
    <dict> 
    <key>example.domain.com</key> 
    <dict> 
     <key>NSIncludesSubdomains</key> 
     <true/> 
     <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
     <true/> 
     <key>NSTemporaryExceptionMinimumTLSVersion</key> 
     <string>TLSv1.1</string> 
    </dict> 
    </dict> 
</dict> 
0

對於那些未來可能會碰到這種情況的人。我遇到了同樣的問題,原因是SSL證書是自簽名的(開發服務器)。 iOS 9 Cordova(UIWebView - 不確定關於WKWebView)會默默地失敗並返回readyState:0,responseText:「」,狀態:0

Android 7沒有此問題。