자바스크립트

fetch 사용하기

javascript0108 2023. 4. 22. 14:46

오늘은 fetch를 사용해보겠습니다.

 

1. fetch란?

fetch는 주로 api 형식으로 사용합니다.

 

2. fetch 사용 예제

1. json

fetch('data.json')
  .then(response => response.json())
  .then(data=>{console.log(data)})

2. xml

fetch('data.xml')
  .then(response => response.text())
  .then(str => new window.DOMParser().parseFromString(str, "text/xml"))
  .then(data => {console.log(data)})

3. 마무리

 

오늘도  짧은 글 읽어주셔서 감사합니다!