Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Created October 5, 2017 16:14
Show Gist options
  • Select an option

  • Save Sanix-Darker/31311b28e735f601d0b217c45b01218d to your computer and use it in GitHub Desktop.

Select an option

Save Sanix-Darker/31311b28e735f601d0b217c45b01218d to your computer and use it in GitHub Desktop.
[JS] XmlhttpRequest
function onProgress(event) {
if (event.lengthComputable) {
var percentComplete = (event.loaded / event.total)*100;
console.log("Envoi en cours: %d%%", percentComplete);
} else {
// Impossible de calculer la progression puisque la taille totale est inconnue
}
}
function onError(event) {
console.error("Une erreur " + event.target.status + " s'est produite au cours de la réception du document.");
}
function onLoad(event) {
// Ici, this.readyState égale XMLHttpRequest.DONE .
if (this.status === 200) {
console.log("Réponse reçu: %s", this.responseText);
//================== C'EST ICI QUE TU PEUX GERER AVEC TON WAY DE ALERT OU FAIRE TON ACTION ===============
} else {
console.log("Status de la réponse: %d (%s)", this.status, this.statusText);
}
}
function onLoadEnd(event) {
// Cet événement est exécuté, une fois la requête terminée.
console.log("Le transfert est terminé. (peut importe le résultat)");
//-----------------------------------------OU AUSSI ICI -------------------------------------------------
//================== C'EST ICI QUE TU PEUX GERER AVEC TON WAY DE ALERT OU FAIRE TON ACTION ===============
//-----------------------------------------OU AUSSI ICI -------------------------------------------------
}
var request = new XMLHttpRequest();
var formData = new FormData();
formData.append("example", $('#example').val());
// Tous ces call back vont appeller les methodes declarees en haut
request.onprogress = onProgress;
request.onerror = onError;
request.onload = onLoad;
request.onloadend = onLoadEnd;
req.open('POST', 'http://www.ddark.com', true);
req.send(formData);
@DDarkBC
Copy link

DDarkBC commented Oct 5, 2017

TY Sanix bro !!!

@Sanix-Darker
Copy link
Author

No problems !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment