How to invoke a simple GET Request using XHR without ANY fancy JavaScript Framework (React, Vue, Angular etc)
Source code:
function getData() {
var xhttp = new XMLHttpRequest();
var targetUrl = "https://pokeapi.co/api/v2/pokemon/pikachu";
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}else if (this.status){
document.getElementById("demo").innerHTML = "Can not access: " + targetUrl
+ "
Response: " + this.status + " " + this.responseText;
}else{
document.getElementById("demo").innerHTML = "Loading... ";
}
};
xhttp.open("GET", targetUrl, true);
xhttp.send();
}
XHR (XMLHttpRequest) GET Request Example