50 lines
1.1 KiB
HTML
50 lines
1.1 KiB
HTML
<html>
|
|
<head>
|
|
<title>ASynchronous AJAX</title>
|
|
|
|
</head>
|
|
<body onload="main();">
|
|
<script type="text/JavaScript">
|
|
|
|
let xmlHttp = new XMLHttpRequest();
|
|
|
|
function requestCallbackFunction(){
|
|
if(xmlHttp.status === 200){
|
|
writeToContent(xmlHttp.responseText);
|
|
|
|
let data = JSON.parse(xmlHttp.responseText);
|
|
console.log(data);
|
|
|
|
setTimeout(null, 1000);
|
|
|
|
data['someProperty'] = { data:"This is an additional value"};
|
|
|
|
console.log(JSON.stringify(data));
|
|
}
|
|
}
|
|
|
|
function getJSONASynchronous(url){
|
|
if(xmlHttp !== null){
|
|
xmlHttp.onload = requestCallbackFunction;
|
|
xmlHttp.open("GET", url, true);
|
|
xmlHttp.send(null);
|
|
}
|
|
}
|
|
|
|
function writeToContent(data){
|
|
document.getElementById("content").innerText = data;
|
|
}
|
|
|
|
function main(){
|
|
getJSONASynchronous("https://google.ca");
|
|
getJSONASynchronous("https://reqbin.com/echo/get/json");
|
|
getJSONASynchronous("./data.json");
|
|
}
|
|
</script>
|
|
|
|
<div id="content">
|
|
|
|
</div>
|
|
|
|
</body>
|
|
</html> |