Skip to content

Instantly share code, notes, and snippets.

@gr0g
Forked from gladiatorAsh/Ajax Setup
Created February 27, 2017 13:51
Show Gist options
  • Save gr0g/032bb9a25edd7dbcce1c6185054f9af8 to your computer and use it in GitHub Desktop.
Save gr0g/032bb9a25edd7dbcce1c6185054f9af8 to your computer and use it in GitHub Desktop.
Ajax Setup
$.ajaxSetup({
cache: false,
tryCount: 0,
retryLimit: 3,
error: function (xhr, textStatus, errorThrown) {
$.unblockUI();
Spin.stopSpin();
// lang = "HI";
if (xhr.status == 404) {
console.log("Service not found");
toastr.error("Something went wrong!! Please try again later ", "Service Error");
return;
}
if (xhr.status == 401) {
console.log("Unauthorized");
toastr.error("You are not authorized to perform this operation", "Service Error");
return;
}
if (xhr.status == 400) {
console.log("Bad Request");
toastr.error("Error occurred while processing the request. Please try again later ", "Service Error");
return;
}
if (textStatus == "timeout") {
/**
* If timeout occurs, retry; default no. of retries are 3
* After 3 retries, exit and show error.
*/
this.tryCount++;
if (this.tryCount < this.retryLimit) {
$.ajax(this);
return;
}
else {
console.log('Repeated timeout error');
toastr.error("Request timeout. Please try after some time.", "Service Error");
return;
}
if (xhr.status == 500) {
console.log("Internal server error");
toastr.error("Error occurred while processing the request , Please try again later.", "Service Error");
return;
} else {
console.log("Please contact the administrator");
toastr.error("Error occurred while processing the request.", "Service Error");
return;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment