Skip to content

Instantly share code, notes, and snippets.

@Christopher2K
Last active April 9, 2018 21:15
Show Gist options
  • Save Christopher2K/05ab8c6853199ad311db1a2d3aea0e15 to your computer and use it in GitHub Desktop.
Save Christopher2K/05ab8c6853199ad311db1a2d3aea0e15 to your computer and use it in GitHub Desktop.
Exemple XHR
// Initialisation de la requête XHR
const request = new window.XMLHttpRequest();
// Options de la requête
const options = {
method: 'GET',
url: 'http://myurl.com',
async: true
};
request.open(options.method, options.url, options.async);
// Paramétrage du callback de succès
request.onload = () => {
if (request.status >= 200 && request.status < 400) {
window.alert('SUCCESS !');
console.log(request.data);
} else {
window.alert('ERROR FROM TARGET SERVER');
}
};
// Paramétrage du callback d'erreur JS
request.onerror = () => {
window.alert('ERROR FROM XHR');
};
// Envoi de la requte
request.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment