AJAX 아약스?
축구팀 아약스는 아는데 ?

하면 이렇게 맞으려나
비동기 방식이란?

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact List</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: center;
}
img {
width: 80px;
height: 80px;
object-fit: cover;
}
</style>
</head>
<body>
<h2>연락처 목록</h2>
<table id="contactsTable">
<thead>
<tr>
<th>번호</th>
<th>이름</th>
<th>전화번호</th>
<th>주소</th>
<th>사진</th>
<th>삭제</th>
</tr>
</thead>
<tbody>
<!-- AJAX를 통해 데이터가 동적으로 추가될 부분 -->
</tbody>
</table>
<script>
// 2단계에서 AJAX 요청을 추가할 예정
</script>
</body>
</html>
이제 script 태그 안에 AJAX 요청을 추가하여 데이터를 가져오도록 합니다.
<script>
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'https://sample.bmaster.kro.kr/contacts?pageno=3&pagesize=10',
dataType: 'json', // JSON 데이터로 응답을 받을 것
success: function (response) {
console.log("데이터 불러오기 성공:", response);
// 테이블의 tbody 요소 가져오기
let tableBody = $("#contactsTable tbody");
// 데이터를 반복하여 테이블에 추가
response.contacts.forEach(function (contact) {
let row = `
<tr>
<td>${contact.no}</td>
<td>${contact.name}</td>
<td>${contact.tel}</td>
<td>${contact.address}</td>
<td><img src="${contact.photo}" alt="사진"></td>
<td><button class="delete-btn">삭제</button></td>
</tr>
`;
tableBody.append(row);
});
// 삭제 버튼 이벤트 추가
$(".delete-btn").click(function () {
$(this).closest("tr").remove();
});
},
error: function (error) {
console.log("데이터 불러오기 실패:", error);
}
});
});
</script>
.html로 저장 (예: contacts.html)✅ 최종 정리
✔ AJAX 요청으로 JSON 데이터 가져오기
✔ 테이블에 데이터 추가 (append)
✔ 삭제 버튼으로 행 삭제 (.closest("tr").remove())
✔ 오류 발생 시 console.log로 확인