Last active
September 2, 2020 05:58
-
-
Save wahyusa/63c2f19fc1bf9b02c0613a94c095a63b to your computer and use it in GitHub Desktop.
Basic Pure Plain Ajax Request Example to Get Data From Github API (Read Json Data Through Pure Plain Ajax)
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
<!DOCTYPE html> | |
<html> | |
<body> | |
<form action="" method="GET"> | |
<button id="btn">Get Data</button> | |
</form> | |
<pre id="result"></pre> | |
<span id="json_selected" style="background:yellow;color:red;font-weight:bold"></span> | |
<script> | |
const btn = document.querySelector("#btn"); | |
btn.onclick = function () { | |
event.preventDefault(); | |
var fulltext_result = document.querySelector("#result"); | |
var json_selected = document.querySelector("#json_selected"); | |
var machine = new XMLHttpRequest(); | |
machine.open("GET", "https://api.github.com/users/wahyusa", true); | |
machine.onreadystatechange = function () { | |
if (this.readyState == 4 && this.status == 200) { | |
// response by text | |
var textonly = this.responseText; | |
fulltext_result.innerHTML = textonly; | |
// get data from json | |
var response = JSON.parse(this.responseText); | |
// get github url | |
var github_url = response.html_url; | |
// get username | |
var username = response.login; | |
json_selected.innerHTML = "User : "+username+"<br>URL : "+github_url; | |
} | |
}; | |
machine.send(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment