Skip to content

Instantly share code, notes, and snippets.

@gladiatorAsh
Created June 18, 2015 10:52
Show Gist options
  • Save gladiatorAsh/ac4a365d54bdfb0cc580 to your computer and use it in GitHub Desktop.
Save gladiatorAsh/ac4a365d54bdfb0cc580 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;
}
}
}
});
@gladiatorAsh
Copy link
Author

Ajax Settings + Retry times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment