Replacement function for jQuery .post 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 &amp;&amp; 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.
I would:


Use a FormData instead of creating a form element. 
Give it a name better than "postR", like... simply "post"?
Add an error handler to the $.ajax call
Unify both $.ajax calls


Pseudo code (Not tested):

function post(url, params, redir = false, callback = null) {
  var formData = new FormData();

  for (var key in params) {
    formData.append(key, value);
  }

  $.ajax({
    type: "POST",
    url: url,
    data: formData,
    success: function(data) {
      typeof(callback) === "function" &amp;&amp; callback(data);
      redir &amp;&amp; window.location.reload();
    }
  });
}