2013-02-13 12 views
5

Sto provando a chiamare Petfinder.com per ottenere un elenco dei nostri animali domestici. L'URL è http://api.petfinder.com/shelter.getPets?key=xxxxx&id=CA1469&format=jsonJQuery chiamata ajax ottenendo codice di stato 0 "errore"

L'URL sembra restituire la multa JSON. Ma quando provo a fare la chiamata sto ricevendo "errore" e codice di stato 0. Ho provato a utilizzare jsonp che si traduce in uno stato di 200 ma un errore di analisi. Se cambio in xml ho il risultato di stato 0 e "errore".

$.ajax({ 

     url: "http://api.petfinder.com/shelter.getPets?key=xxxx&id=CA1469&format=json", 
     dataType: "json", 
     type: "GET", 
     success: function (data) { 
      alert("hi"); 
     }, 
     error: function (jqXHR, exception) { 
      if (jqXHR.status === 0) { 
       alert('Not connect.\n Verify Network.'); 
      } else if (jqXHR.status == 404) { 
       alert('Requested page not found. [404]'); 
      } else if (jqXHR.status == 500) { 
       alert('Internal Server Error [500].'); 
      } else if (exception === 'parsererror') { 
       alert('Requested JSON parse failed.'); 
      } else if (exception === 'timeout') { 
       alert('Time out error.'); 
      } else if (exception === 'abort') { 
       alert('Ajax request aborted.'); 
      } else { 
       alert('Uncaught Error.\n' + jqXHR.responseText); 
      } 

     } 
    }); 
+1

Si verifica un errore 'Same Origin' nella console? Stai usando IE? Le richieste di origine incrociata devono essere inviate come JSONP (che richiede una risposta JSONP corretta) o con CORS abilitato che richiede al server di supportare CORS e il browser per supportare CORS. jQuery non supporta CORS in IE <10. –

+0

La libreria client API di Trello ha un buon esempio di come gestiscono il supporto CORS: https://trello.com/1/client.coffee – forivall

+0

possibile duplicato di [Ajax jqXHR.status == 0 correzione errore] (http://stackoverflow.com/questions/23802474/ajax-jqxhr-status-0-fix-error) –

risposta

1

si sta eseguendo in un XSS problema CORS. La soluzione migliore consiste nell'impostare un proxy sul server Web e instradare la chiamata AJAX attraverso di essa.

+1

Come citato da KevinB; Penso che intendessi CORS, non XSS – Alexander

+0

concordato. –

+0

Sto usando AWS Api Gateway per la mia applicazione ... e ricevendo questo stesso problema ... puoi suggerire una politica CORS preferita in modo che questo errore venga risolto. Lo sto testando ora ryte usando una semplice chiamata ajax che viene eseguita sull'evento 'onload' del tag body –

2

È necessario assicurarsi che quando il server invia la risposta di errore, invia anche intestazioni CORS appropriate insieme ad esso.

Se il browser non consente al codice JavaScript di visualizzare alcuna informazione proveniente da quel dominio, anche il codice di stato.

Problemi correlati