2015-09-22 15 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); 
    } 
}); 

E il risultato che ottengo è:Ajax non funziona in IOS 9,0 Cordova

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

provo aggiungere intestazione a mio php, ma ancora non funziona. Il codice ajax funziona prima di aggiornare il mio xcode a 7.0 e il simulatore di ios a 9.0.

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

risposta

12

Per quanto ho capito tutta l'ATS (App Transport Security - iOS 9) cosa, il metodo raccomandato da area28 non dovrebbe essere quello che si sta utilizzando all'interno di un'applicazione.

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

Questo permetterà tutte le richieste esterne ad ogni dominio ciò che non è definitivamente il modo in cui si dovrebbe usare. A mio parere si dovrebbe definire un nuovo <dict> all'interno del vostro info.plist e aggiungere questo codice ad essa (per modificare il info.plist si può semplicemente utilizzare un normale editor di testo come testo sublime, ecc):

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

Questo permetterà solo le richieste al dominio specificato. Il modo descritto è quello che apple introduced on the WWDC 2015. Come puoi vedere sullo screenshot, è il modo in cui Apple vuole che gli utenti lo usino.

Se non è stato specificato nulla, si otterrà

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.

Quindi, cambiarlo come ho detto e l'errore è andato via. enter image description here

+0

Sono d'accordo. Entrambe funzioneranno e potrai diventare più specifico se necessario. – area28

+1

Non puoi ** puoi **, devi ** essere ** più specifico! Questi sono argomenti di sicurezza che non sono discutibili a mio parere. – Sithys

+0

Il secondo approccio con la whitelist di ogni singolo dominio è sicuramente la strada da percorrere, il primo non funziona in iOS prima di iOS9.(Testato su iOS 7) – maechler

5

Se si lavora in un progetto Xcode potrebbe essere necessario modificare il file info.plist. La sicurezza è cambiata con iOS9. Apple consiglia vivamente di utilizzare https per le richieste Web quando possibile. Here è una risposta correlata a questo problema.

Da quel post:

Aggiungi questo al vostro file 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

Grazie per questo, funziona ~ – JohnLoong

+1

Questo ha funzionato per me. La mia app non è stata caricata dopo l'aggiornamento del dispositivo su iOS9, ora funziona come un incantesimo. Life Saver, grazie! –

1

Per me è stata una combinazione di risposta @ area28 e @Sithys. Così ho finito per aggiungere questo al mio 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

Per quelli di voi che potrebbero imbattersi in questo in futuro. Ho avuto lo stesso problema e si è scoperto che il certificato SSL è autofirmato (server di sviluppo). iOS 9 Cordova (UIWebView - non sicuro su WKWebView) fallirebbe in modo silenzioso e restituire readyState: 0, responseText: "", stato: 0

Android 7 non aveva questo problema.

Problemi correlati