JQuery S.Ajax Handler Error Eseguited se ReadyState = 4 e Status = 200
-
27-10-2019 - |
Domanda
Sto facendo una chiamata $ .ajax, che sta restituendo una risposta JSON e sembra tutto a posto, ma invece che il gestore di successo venga invocato, il gestore degli errori di $ .ajax viene invocato anche se ReadyState = 4 e lo stato = 200.
La chiamata $ .ajax è:-
$.ajax({
url: 'inc/ajax_printorder.asp',
type: "GET",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (sendresponse) {
var message = (typeof sendresponse.jsonresp) == 'string' ? eval('(' + sendresponse.jsonresp + ')') : sendresponse.jsonresp;
if (message[0].ok == '1') {
var order = window.open('', 'PrintWindow', 'width=600,height=600');
var html = '<html><head><title>Print Your Order</title></head><body><div id="myprintorder">' + $('<div />').append(message[0].msg) + '</div></body></html>';
order.document.open();
order.document.write(html);
order.document.close();
return false;
};
},
error: function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
E da Firebug, la risposta dell'Ajax è:-
{"jsonresp":[{"__type":"sendresponse","ok":"1","msg":"<h1>Your www.sandwichlunchesnewbury.co.uk order on 02/05/2011 00:34:01</h1><p>Website order from www.sandwichlunchesnewbury.co.uk on 02/05/2011 00:34:01 from:- </p><table width="60%" style="border:1px solid blue;padding:5px;"><tr><td>Name</td><td> a </td></tr><tr><td>Phone</td><td> b </td></tr><tr><td>Email</td><td> </td></tr><tr><td>Business name</td><td> </td></tr><tr><td>Delivery address</td><td> c </td></tr><tr><td>Date food required</td><td> Monday, 02/05/2011 </td></tr><tr><td>Time food required</td><td> 10 Am </td></tr><tr><td> </td><td> </td></tr><tr><td>Just Sandwiches (standard)</td><td> </td></tr><tr><td>Just Sandwiches (gourmet)</td><td> </td></tr><tr><td>Just Baguettes (standard)</td><td> </td></tr><tr><td>Just Baguettes (gourmet)</td><td> </td></tr><tr><td>Gourmet Bread Sandwiches</td><td> 2 </td></tr><tr><td>Sausage rolls</td><td> </td></tr><tr><td>Cookie boxes</td><td> </td></tr><tr><td> </td><td> </td></tr><tr><td>Total price</td><td> £50.00 </td></tr><tr><td> </td><td> </td></tr><tr><td colspan="2">Other info & special instructions </td></tr><tr><td colspan="2"> </td></tr></table>"}]}
Qualche pensiero sul perché sta per errore piuttosto che successo?
Grazie
EPX
Soluzione
La tua risposta JSON non è valida, tra le altre cose, dovrai sfuggire alle doppie citazioni (\"
invece di "
). Non hai pubblicato quale testo di errore (il valore di err
come passato a error
) è ma sospetto che lo sarà parsererror
.
ho trovato Jsonlint Per essere molto utile per garantire che sto ricevendo/invio JSON valido.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow