2015-09-22 10 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(のApp交通安全 - iOSの9)理解されるような事を、area28から推奨される方法は、アプリケーション内で使用しているものであってはなりません。

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

これは決定的にあなたがそれを使用すべき方法ではありませんどのようなすべてのドメインへのすべての外部要求を許可します。 (info.plistあなただけの崇高なテキストなどのような通常のテキストエディタを使用することができます編集する)私の意見では、あなたのinfo.plist内に新しい<dict>を定義する必要があり、それには、このコードを追加します。

<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

すべての単一ドメインをホワイトリストに入れるという第2のアプローチは間違いなく、最初のものはiOS9より前のiOSでは動作しません。(iOS 7でテスト済み) – maechler

5

あなたがinfo.plistファイルを編集する必要がありXcodeプロジェクトで作業している場合。セキュリティはiOS9で変更されました。可能であれば、AppleはWebリクエストに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さんと@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証明書が自己署名(開発サーバー)されていることが判明しました。 readyState:0、responseText: ""、ステータス:0

Android 7にはこの問題はありませんでしたが、iOS 9 Cordova(UIWebView - WKWebViewについてはわかりません)は黙って失敗します。

関連する問題