$.fn.serializeObject = function ()
{
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
function sendForm(form, data) {
return new Promise((fulfill, reject) => {
if (!data)
data = $(form).serializeObject();
// console.log(`${form.method} ${form.action}`);
var ajaxSettings = {
method: form.method,
url: form.action,
data: JSON.stringify(data),
// dataType: form.method === 'get' ? 'jsonp' : 'json',
dataType: 'json',
contentType: 'application/json',
processData: false,
xhrFields: {
withCredentials: true
}
};
var ajaxRequest = $.ajax(ajaxSettings);
ajaxRequest.done((data, textStatus) => {
console.log(textStatus);
console.log(JSON.stringify(data, null, 2));
fulfill(data);
});
ajaxRequest.fail((response, textStatus, errorThrown) => {
console.log(textStatus);
console.log(errorThrown);
if (response.status === 401) {
screenLogin();
} else if (response.responseText) {
var data = JSON.parse(response.responseText);
console.log(JSON.stringify(data, null, 2));
} else {
console.log(errorThrown);
}
reject(data);
});
});
}