Asynchronous JavaScript and XML == 비동기적 자바스크립트와 xml

<!-- https://www.mockaroo.com : 임시로 데이터파일 만들어주는 툴 -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<!-- Bootstrap CSS v5.2.1 -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
</head>
<body>
<header>
<!-- place navbar here -->
</header>
<main>
<div class="container-fluid">
<div class="row justify-content-center align-items-center g-2">
<div class="col-6">Column</div>
<div>
<div class="table-responsive">
<table class="table table-secondary">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Product Name</th>
<th scope="col">Price</th>
<th scope="col">Options</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<div class="col-6">Column</div>
</div>
</div>
</main>
<footer>
<!-- place footer here -->
</footer>
<!-- Bootstrap JavaScript Libraries -->
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
crossorigin="anonymous"
></script>
<script>
const load = () =>{
const xmlhttp = new XMLHttpRequest(); // 1. create XML HTTP Request object
//onREadyStateChange will call the function each time the readyState is changing
//onLoad will call the function when readyState is 4 - meaning the request has finished and response is ready
//xmlhttp.onLoad =() =>{
xmlhttp.onreadystatechange = () =>{ // 2. create the call back function using either onReadyStateChange or onLoad
console.log(xmlhttp.readyState); //important methods for xmlhttp are : 1. readyState
console.log(xmlhttp.status);// 2. status
if(xmlhttp.status == 200){
console.log(xmlhttp.responseText);
}else{
console.log(xmlhttp.statusText);
}
}
xmlhttp.open("GET","http://127.0.0.1:5500/JS/Day4/products_file.json") //loopback address
xmlhttp.send();
}
load();
</script>
</body>
</html>
const load = () =>{
const xmlhttp = new XMLHttpRequest(); // 1. create XML HTTP Request object
//onREadyStateChange will call the function each time the readyState is changing
//onLoad will call the function when readyState is 4 - meaning the request has finished and response is ready
//xmlhttp.onLoad =() =>{
xmlhttp.onreadystatechange = () =>{ // 2. create the call back function using either onReadyStateChange or onLoad
console.log(xmlhttp.readyState); //important methods for xmlhttp are : 1. readyState
console.log(xmlhttp.status);// 2. status
if(xmlhttp.status == 200){
console.log(xmlhttp.responseText);
}else{
console.log(xmlhttp.statusText);
}
}
xmlhttp.open("GET","http://127.0.0.1:5500/JS/Day4/products_file.json") //loopback address
xmlhttp.send();
}
load();
xmlhttp.onload = function() {
if (xmlhttp.status == 200) {
console.log(xmlhttp.responseText); // 요청 성공 시 응답 출력
} else {
console.error("Error: " + xmlhttp.statusText);
}
};
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText); // 요청 성공 시 응답 출력
}
};