Last active
April 9, 2018 21:15
-
-
Save Christopher2K/05ab8c6853199ad311db1a2d3aea0e15 to your computer and use it in GitHub Desktop.
Exemple XHR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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