Commit 0de3b2e6 authored by Mat's avatar Mat

Ajax, une seule fonction avec les vérifs de statut

* une seule fonction pour les reqêtes ajax get, post et delete
* déplace les conditions readyState et status du côté de la fonction la
fonction
* rem: pour read et update, on a besoin de passer un argument request
qui remplace le this à l'intérieur !!
parent 69153edf
......@@ -74,13 +74,10 @@
{
tbody.innerHTML = null;
getAJAX(ownersURL, function()
AJAX('GET', ownersURL, function(request)
{
if (this.readyState === 4
&& (this.status === 200 || this.status === 0)) {
// Récupère le json et boucle pour créer le tableau
let json = JSON.parse(this.responseText);
let json = JSON.parse(request.responseText);
for (let i = 0; i < json.length; i++)
{
......@@ -113,22 +110,15 @@
{
let ownerId = this.dataset.ownerId;
submitURL = Routing.generate('api_owner_update', {'id': ownerId});
getAJAX(submitURL, function()
AJAX('GET', submitURL, function(request)
{
if (this.readyState === 4)
{
if (this.status === 200 || this.status === 0)
{
let json = JSON.parse(this.responseText);
let json = JSON.parse(request.responseText);
form.querySelector('#owner_firstname').value = json.firstname;
form.querySelector('#owner_lastname').value = json.lastname;
form.parentNode.classList.remove('hidden');
}
else if (this.status === 400) {
handleErrors(this);
}
}
});
});
td4. appendChild(editBtn);
......@@ -142,18 +132,11 @@
let ownerId = this.dataset.ownerId;
let r = confirm('Are you sure ?');
if (r === true) {
submitURL = Routing.generate('api_owner_delete_one', {'id': ownerId});
deleteAJAX(submitURL, function()
{
if (this.readyState === 4) {
if (this.status === 200 || this.status === 0)
{
AJAX('DELETE', submitURL, function() {
reloadTableOwner();
}
else if (this.status === 400) {
handleErrors(this);
}
}
});
}
});
......@@ -163,7 +146,6 @@
tbody.appendChild(tr);
}
}
});
}
reloadTableOwner();
......@@ -196,24 +178,13 @@
{
let form = this;
e.preventDefault(); // Annule l'événement, mais préserve la fonction callback
postAJAX(submitURL, function()
AJAX('POST', submitURL, function()
{
if (this.readyState === 4)
{
if (this.status === 200 || this.status === 0)
{
reloadTableOwner();
form.parentNode.classList.add('hidden');
} else if (this.status === 400) {
handleErrors(this);
} else if (this.status === 500) {
console.log(this.status + ' ' + this.statusText);
}
}
}, form);
},
form );
});
......
......@@ -8,29 +8,9 @@ function formatStringDate(string)
return formatDate(new Date(string));
}
function getAJAX(url, callback)
{
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = callback;
xhr.open('GET', url, true);
xhr.send(null);
}
function postAJAX(url, callback, post)
{
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = callback;
xhr.open('POST', url, true);
xhr.send(new FormData(post));
}
function deleteAJAX(url, callback)
{
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = callback;
xhr.open("DELETE", url, true);
xhr.send(null);
}
/*
* F O N C T I O N S A J A X
*/
function handleErrors(response)
{
......@@ -55,3 +35,38 @@ function handleErrors(response)
}
alert(message);
}
function AJAX(type, url, successCallBack, post = null)
{
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
//console.log(xhr.readyState);
if (xhr.readyState === 4)
{
//console.log(xhr.status);
if (xhr.status === 200 || xhr.status === 0)
{
//console.log('success');
successCallBack(xhr);
}
else if (xhr.status === 400)
{
handleErrors(xhr);
}
}
};
xhr.open(type, url, true);
xhr.send((post === null)? null : new FormData(post));
}
// else if (this.status === 500)
// {
// console.log(this.status + ' ' + this.statusText);
// }
// if (post !== null) {
// xhr.send(new FormData(post));
// } else {
// xhr.send(null);
// }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment