Today I just completed a personal POST function that emulates jQuery's using $.ajax, but also implementing a possible redirect. I just finished up and it looks like it is functional, but since this is something I want to keep long term and integrate in many projects, I want to make sure it is as functional as possible.
function postR(url, params, redir = false, callback = null) {
if(redir){
var form = document.createElement('form');
form.method = 'post';
form.action = url;
for (var key in params) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = params[key];
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
}else{
if(callback && typeof(callback) === "function"){
$.ajax({
type: "POST",
url: url,
data: params,
success: function(data){
callback(data)
}
});
}else{
$.ajax({
type: "POST",
url: url,
data: params
});
}
}
}
The purpose for building this was because I wanted to use jQuery's post function to do some JS work with a form before it was submitted, but also wanted to redirect alongside the POST request, like a natural form.