Last active
September 11, 2020 13:56
-
-
Save petrusnog/5836496359b4f4176003fa4fb112fdc6 to your computer and use it in GitHub Desktop.
My first experience using Ajax with pure JavaScript.
This file contains hidden or 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
var httpRequest; | |
if (window.XMLHttpRequest){ | |
httpRequest = new XMLHttpRequest(); | |
} else if (window.ActiveXObject) { | |
httpRequest = new ActiveXObject(); | |
} | |
httpRequest.onreadystatechange = ajaxResponse; | |
httpRequest.open("POST", "localhost/ajax", true); | |
httpRequest.send(); | |
function ajaxResponse () { | |
//Checking request status | |
/* ReadyState is equivalent to the state of the request, it is who informs | |
to our code if the server's response is ready to be | |
consumed. */ | |
if (httpRequest.readyState === 4) { | |
//Checking the status of the HTTP request | |
/* status returns a code that is equivalent to the status list of the | |
HTTP request, this list refers to the behavior of that | |
communication with the server, and indicates whether it was successful, | |
or if there was any impediment, change of protocol, etc ... */ | |
if (httpRequest.status === 200) { | |
//SUCESSFUL REQUEST | |
}else{ | |
/*There is something wrong with the request, maybe it has | |
returned a 404, which says "Not found". */ | |
/*I ask the javascript to print the HTTP status on the console, | |
as a log. */ | |
console.log(httpRequest.status); | |
} | |
}else{ | |
//Request is not yet ready or was unsuccessful. | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment