function ajax(url, successCallback, failureCallback = () => {}) { fetch(url, { method: 'GET', redirect: 'manual', headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' } }) .then(response => { if (response.ok) { return response.json(); } else if (response.type == 'opaqueredirect') { window.location.href = "/login"; throw new Error('Sesion expirada'); } else { throw new Error('Cerciórese de tener Internet. Si el error continúa comuníquese con el área de soporte'); } }) .then(data => { if (data.status == 'OK') { successCallback(data.record, data); } else { //alert(data.msg); failureCallback(data.msg, data); } }) .catch(error => { //alert(error); failureCallback(error.message, {}); }); } function ajaxPost(url, formData, successCallback, failureCallback = () => {}) { fetch(url, { method: 'POST', body: (formData instanceof FormData ? formData : Object.keys(formData).reduce((fd, key) => { fd.append(key, formData[key]); return fd; }, new FormData())), redirect: 'manual', headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' } }) .then(async (response) => { if (response.ok) { try { return await response.json(); } catch (e) { throw new Error('No se pudo procesar la solicitud. Nuestro equipo comenzará a revisar lo que ocurrió.'); } } else if (response.type == 'opaqueredirect') { window.location.href = "/login"; throw new Error('Sesion expirada'); } else { throw new Error('Cerciórese de tener Internet. Si el error continúa comuníquese con el área de soporte'); } }) .then(data => { if (data.status == 'OK') { successCallback(data.record, data); } else { //alert(data.msg); failureCallback(data.msg, data); } }) .catch(error => { //alert(error); failureCallback(error.message, {}); }); }