갈푸라떼
갈푸라떼는 개발중
갈푸라떼
전체 방문자
오늘
어제
  • 분류 전체보기 (232)
    • CS (0)
      • CSinfo (0)
    • Frontend (15)
      • HTML,CSS (1)
      • Javascript (2)
      • React (0)
      • React Hook (12)
    • Backend (0)
      • Python (0)
      • Node.js (0)
      • php (0)
    • DB (2)
      • MySQL (2)
      • BigQuery (0)
      • Mongodb (0)
    • Study (186)
      • JavaScript (72)
      • JavaScript(Clean code) (50)
      • Node.js (11)
      • HTML,CSS (13)
      • React (30)
      • TypeScript (10)
      • React-Native (0)
    • Error (2)
      • error (2)
    • Git (22)
      • Git (22)
    • Help Coding (4)
      • Useful websites (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • Github

공지사항

인기 글

태그

  • ECMAScript
  • 상속
  • 객체타입
  • 실행 컨텍스트
  • 정적 레벨
  • 이터러블
  • 심볼
  • 스코프 체인
  • this
  • function
  • 네이밍
  • PM2
  • 오버라이딩
  • class
  • 싱글스레드
  • 프로토타입
  • 함수
  • 컴파일러
  • 렉시컬 환경
  • Arrow
  • 자바스크립트엔진
  • Babel
  • structure
  • 인터프리터
  • prototype
  • SPREAD
  • 호이스팅
  • 원시타입
  • nodemon
  • symbol

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
갈푸라떼

갈푸라떼는 개발중

Study/JavaScript

[JavaScript_study] fetch api

2022. 4. 13. 02:57

* fetch api 사용법

fetch() 함수는 첫번째 인자로 URL, 두번째 인자로 옵션 객체를 받고, Promise 타입의 객체를 반환합니다. 반환된 객체는, API 호출이 성공했을 경우에는 응답(response) 객체를 resolve하고, 실패했을 경우에는 예외(error) 객체를 reject합니다.

 

fetch(url, options)
  .then((response) => console.log("response:", response))
  .catch((error) => console.log("error:", error));

 

* fetch api 특징

  • 브라우저 api중 하나이다.
  • 비동기의 대표적인 api이다. 서버와 통신할 수 있는 api이다.
  • .json()메서드는 이름은 .json이지만 실제로는 Object로 반환해준다.
  • .json()은 Promise를 return한다.

 * fetch 예시 코드

  • 날씨 정보 요청 api를 이용
  • https://www.7timer.info/bin/astro.php?lon=113.2&lat=23.1&ac=0&unit=metric&output=json&tzshift=0
<!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>
      fetch(
        'https://www.7timer.info/bin/astro.php?lon=113.2&lat=23.1&ac=0&unit=metric&output=json&tzshift=0'
      )
        .then((response) => {
          return response.json();
        })
        .then((data) => console.log(data.dataseries));
    </script>
  </body>
</html>

 

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

[JavaScript_study] 가비지 컬렉션 (garbage collection)  (0) 2022.04.14
[JavaScript_study] 스코프(Scope)  (0) 2022.04.14
[JavaScript_study] JSON: JavaScript Object Notation  (0) 2022.04.13
[JavaScript_study] async/await  (0) 2022.04.13
[JavaScript_study] Promise static method  (0) 2022.04.13
    'Study/JavaScript' 카테고리의 다른 글
    • [JavaScript_study] 가비지 컬렉션 (garbage collection)
    • [JavaScript_study] 스코프(Scope)
    • [JavaScript_study] JSON: JavaScript Object Notation
    • [JavaScript_study] async/await
    갈푸라떼
    갈푸라떼

    티스토리툴바