ajax
callback
Ricerca…
Interpretazione degli errori con il callback "errore"
Gli errori, se gestiti correttamente dal server, verranno restituiti al client con uno specifico codice di stato HTTP diverso da 2xx (vedere RFC 2616 sezione 10 ).
Si consiglia di catturare globalmente i propri errori dal proprio $.ajaxSetup() come mostrato nell'esempio sotto. Pertanto, tutti gli errori provenienti dalle chiamate ajax verranno automaticamente interpretati dall'impostazione Ajax.
$.ajaxSetup({
error: function (jqXHR, exception, errorThrown) {
var message;
var statusErrorMap = {
'400': "Server understood the request, but request content was invalid.",
'401': "Unauthorized access.",
'403': "Forbidden resource can't be accessed.",
'500': "Internal server error.",
'503': "Service unavailable."
};
if (jqXHR.status) {
message = statusErrorMap[jqXHR.status];
if (!message) {
message = "Unknown Error.";
}
} else if (exception == 'parsererror') {
message = "Error.\nParsing JSON Request failed.";
} else if (exception == 'timeout') {
message = "Request Time out.";
} else if (exception == 'abort') {
message = "Request was aborted by the server";
} else {
message = "Unknown Error.";
}
// How you will display your error message...
console.log(message);
console.log(errorThrown);
}
});
Si consiglia inoltre di "sovraccaricare" la richiamata di error in uno specifico $.ajax() quando si attende un messaggio di errore specifico.
$.ajax({
url: './api',
data: { parametersObject },
type:'post',
dataType: 'json',
success:function(output){
// Interpret success
},
error: function(xhr,textStatus,ErrorThrown){
// Specific error will not be interpreted by $.ajaxSetup
}
});
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow