const content = document.getElementById("content");
let xhr = new XMLHttpRequest();

xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.send();

xhr.onload = function () {
   if (xhr.status >= 200 && xhr.status < 300) {
      const datas = JSON.parse(xhr.response);
      for(let i in datas){
          let data = document.createElement("h2");
          let text = document.createTextNode(`ID - ${datas[i].id} & Title - ${datas[i].title}`);
          data.appendChild(text);
          content.appendChild(data);
      }
   } else {
      console.log('failed');
   }
};

 

 

 

'JavaScript > ajax' 카테고리의 다른 글

jquery ajax load()  (0) 2021.02.12
xhr (XMLHttpRequest)  (0) 2021.02.11