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);
}
}
});
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. –
La libreria client API di Trello ha un buon esempio di come gestiscono il supporto CORS: https://trello.com/1/client.coffee – forivall
possibile duplicato di [Ajax jqXHR.status == 0 correzione errore] (http://stackoverflow.com/questions/23802474/ajax-jqxhr-status-0-fix-error) –