JavaScript를 사용한 비동기 통신, 클라이언트와 서버 간에 XML데이터를 주고 받는 기술
=> 자바스크립트를 통해서 서버에 데이터를 요청하는 것
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
</head>
<body>
<script>
$(function () {
$('#get_data_button').click(function() {
// GET 방식
// $('#demo').load('ajax_info.txt');
// JSON 파일을 불러온 후 다음 명령을 실행
$.getJSON('people.json', function (json) {
console.log(json);
// json 파일의 내용을 텍스트로 변환하여 div에 입력
$('#demo').html(JSON.stringify(json));
// json 파일의 내용 중 이름과 스킬만 출력하여 입력
$('#demo').html(`${json.name} : ${json.skill}`);
});
});
});
</script>
<div id="demo"></div>
<button id="get_data_button">get data</button>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function loadDoc() {
console.log('start');
// 비동기 == 오래걸리는 동작
fetch('people.json')
// .then(function(response) {
// return response.text();
// })
// 화살표 함수(Arrow function)
// 람다식 (lambda exepression)
.then(response => response.json()) // 기다렸다가 여기서 처리
.then(json => {
console.log(json);
let result = '${json.name} : ${json.skill}';
document.getElementById('demo').innerHTML = result;
document.getElementById('json').innerHTML = JSON.stringify(json);
});
console.log('end');
}
</script>
<div id="demo"></div>
<div id="json"></div>
<button onclick="loadDoc()">get data</button>
</body>
</html>
fetch의 첫 번째 인자 : url
fetch의 두 번째 인자 : 옵션객체
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
</head>
<body onload="loadData()">
<script>
function loadData() {
fetch('https://jsonplaceholder.typicode.com/todos/')
.then(response => response.json())
.then(jsonArray => {
// for (let i = 0; i < jsonArray.length; i++)
let $list = $('#list');
jsonArray.forEach((data, i) => {
$list.append(`<li>${i} : ${data.title}</li>`);
})
console.log(jsonArray);
});
}
</script>
<ul id="list">
</ul>
</body>
</html>
리스트로 구성된 원본 데이터
foreach 문을 활용하여, 원소의 위치와 원소의 일부 내용을 html 리스트에 추가
-- foreach(원소가 될 변수 이름, 원소의 인덱스)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<style>
.card {
/* Add shadows to create the "card" effect */
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
margin-bottom: 10px;
width: 600px;
}
/* On mouse-over, add a deeper shadow */
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
/* Add some padding inside the card container */
.container {
padding: 2px 16px;
}
</style>
</head>
<body onload="loadData()">
<script>
function moveCommentsPage(postId) {
console.log(postId);
// 페이지 이동
document.location.href = `exam4_comments.html?id=${postId}`;
}
function loadData() {
fetch('https://jsonplaceholder.typicode.com/posts/')
.then(response => response.json())
.then(jsonArray => {
// for (let i = 0; i < jsonArray.length; i++)
let $list = $('#list');
jsonArray.forEach((data, i) => {
// 위의 링크에서 받아온 정보, 리스트의 원소를 하나씩 받아서 'card'클래스 div를 추가하며 입력
$list.append(`<div class="card"token interpolation">${data.id})">
<div class="container">
<h4><b>${data.title}</b></h4>
<p>${data.body}</p>
</div>
</div>`);
})
console.log(jsonArray);
});
}
</script>
<div id="list">
<!-- <div class="card">
<div class="container">
<h4><b>John Doe</b></h4>
<p>Architect & Engineer</p>
</div>
</div> -->
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<style>
.card {
/* Add shadows to create the "card" effect */
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
margin: 10px;
width: 330px;
height: 550px;
display: inline-block;
float: left;
}
/* On mouse-over, add a deeper shadow */
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
/* Add some padding inside the card container */
.container {
padding: 2px 16px;
}
img {
width: 100%;
margin-top: 15px;
}
h4 {
text-align: center;
font-size: 20px;
}
</style>
</head>
<body onload="loadData()">
<script>
function moveCommentsPage(id) {
console.log(id);
// 페이지 이동
document.location.href = `exam6_one_movie.html?id=${id}`;
}
function loadData() {
fetch('https://api.themoviedb.org/3/movie/upcoming?api_key=a64533e7ece6c72731da47c9c8bc691f&language=ko-KR&page=1')
.then(response => response.json())
.then(jsonArray => {
// for (let i = 0; i < jsonArray.length; i++)
let movie = jsonArray.results;
movie.forEach((data, i) => {
$('#list').append(`<div class="card"token interpolation">${data.id})">
<div class="container">
<img src="https://image.tmdb.org/t/p/w500${data.poster_path}" alt="poster_image">
<h4><b>${data.title}</b></h4>
</div>
</div>`);
})
console.log(jsonArray);
});
}
</script>
<div id="list">
</div>
</body>
</html>