Get JSON with XMLHttpRequest
from the Github API
by nerdycap007
HTML
GitHub Name:
<input type="text" id="myName" value="pytorch">
<button onclick='request("https://api.twitter.com/1/statuses/oembed.json?url=https://twitter.com/FanaHOVA/status/1242664683527692289");'>Get it</button>
<div id="input">
</div>
JavaScript
function request(url) {
var xReq = new XMLHttpRequest();
xReq.addEventListener("progress", updateProgress);
xReq.addEventListener("load", transferComplete);
xReq.addEventListener("error", transferFailed);
xReq.addEventListener("abort", transferCanceled);
var updateProgress = function() {
console.log("updateProgress");
}
function transferComplete(evt) {
console.log("The transfer is complete.");
}
function transferFailed(evt) {
console.log("An error occurred while transferring the file.");
}
function transferCanceled(evt) {
console.log("The transfer has been canceled by the user.");
}
xReq.open("GET", url);
xReq.setRequestHeader('Content-Type', 'text/plain');
xReq.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
var myArr = JSON.parse(this.responseText);
console.log(myArr);
}
}
xReq.send();
}