Skip to content

Instantly share code, notes, and snippets.

@kdssoftware
Created May 14, 2020 18:36
Show Gist options
  • Save kdssoftware/8072e5006b97b25d0a8aa12f86f7d5f9 to your computer and use it in GitHub Desktop.
Save kdssoftware/8072e5006b97b25d0a8aa12f86f7d5f9 to your computer and use it in GitHub Desktop.
nice ajax post function using bootrstrap
// url: url for the post request
// element: which element the loading sign will be on (creates #loading)
// updateObject: object with the data to sent.
//use a .click() function to trigger this function.
function postAjax(url,element,updateObject){
$("#loading").remove();
$(element).append($("<span id='loading'></span>"));
let maxtwo = 0;
let done = "<i id=\"check\" class=\"fas fa-check\" style=\"margin-left: 15px;\"></i>"
let loading = "<div id=\"spinner\" class=\"spinner-border spinner-border-sm text-#{settings.theme} role=\"status\" style=\"margin-left: 15px;\">\n" +
" <span class=\"sr-only\">Loading...</span>\n" +
"</div>";
let error = "<i id=\"error\" class=\"fas fa-exclamation-triangle\" style=\"margin-left: 15px;\"></i>"
$(document).ajaxSend(function (event, xhr, settings) {
if (settings.url === url && maxtwo!==2) {
$("#loading").append($(loading));
maxtwo++;
}
});
$(document).ajaxError(function (event, xhr, settings) {
if (settings.url === url && maxtwo !== 2) {
$("#spinner").remove();
$("#loading").append($(error));
maxtwo++;
}
});
$(document).ajaxSuccess(function (event, xhr, settings) {
if (settings.url === url && maxtwo !== 2) {
$("#spinner").remove();
$("#loading").append($(done));
maxtwo++;
}
});
$.post(url, updateObject, function (data, status) {
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment