"> Document
"> Document
">

image.png

image.png

image.png

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
    <style>
      div {
        border: 1px solid black;
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div id="post-1"></div>
    </div>

    <script>
      async function download() {
        let res = await fetch("<https://jsonplaceholder.typicode.com/posts/1>");
        console.log(res);

        const contentType = res.headers.get("content-type");
        console.log(contentType);

        let post = await res.json(); // json문자열을 javascript object로 변환
        console.log(post);

        let dom = document.querySelector("#post-1");
        dom.innerHTML = `
            userId : ${post.userId} <br>
            id : ${post.id} <br>
            title : ${post.title} <br>
            body : ${post.body} <br>
        `;
      }

      download();

      //while (true) {}
    </script>
  </body>
</html>

1. 이 코드가 하는 일 (한 문장)

브라우저가 https://jsonplaceholder.typicode.com/posts/1GET 요청을 보내서 글 1개를 받아오고, 그 내용을 #post-1 div에 HTML로 출력한다.


2. 전체 코드 (원본)

<!DOCTYPE html>
<htmllang="en">
<head>
<title>Document</title>
<style>
div {
border:1px solid black;
padding:10px;
      }
</style>
</head>
<body>
<divclass="container">
<divid="post-1"></div>
</div>

<script>
asyncfunctiondownload() {
let res =awaitfetch("<https://jsonplaceholder.typicode.com/posts/1>");
console.log(res);

const contentType = res.headers.get("content-type");
console.log(contentType);

let post =await res.json();// json문자열을 javascript object로 변환
console.log(post);

let dom =document.querySelector("#post-1");
        dom.innerHTML = `
            userId :${post.userId} <br>
            id :${post.id} <br>
            title :${post.title} <br>
            body :${post.body} <br>
        `;
      }

download();

//while (true) {}
</script>
</body>
</html>


3. HTML 구조 설명

3.1 <!DOCTYPE html>

<!DOCTYPE html>


3.2 화면에 출력할 자리 만들기

<divclass="container">
<divid="post-1"></div>
</div>

즉, 나중에 JS가: