์ƒˆ์†Œ์‹

๐Ÿ’ป Web/-- HTTP | Network

Fetch API ์˜ ์‚ฌ์šฉ ๋ฐ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ( ๊ฐœ๋… , ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ , ajax )

  • -

 

*Fetch ๋ž€?

  • Ajax๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋น„๊ต์  ์ตœ๊ทผ์— ๋‚˜์˜จ ๊ธฐ์ˆ .
  • jQuery.ajax()์™€ ๋‹ค๋ฅธ์ ์€ (1) fetch๋กœ๋ถ€ํ„ฐ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๋Š” HTTP error์ƒํƒœ๋ฅผ rejectํ•˜์ง€ ์•Š๋Š”๋‹ค. (2) ์ฟ ํ‚ค๋ฅผ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค๋Š”์ .
  • ์›นํŽ˜์ด์ง€ ์ „์ฒด๋ฅผ ๋ฆฌ๋กœ๋”ฉ(re-loading)ํ•˜์ง€ ์•Š๊ณ , ์ผ๋ถ€๋ถ„๋งŒ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋กœ ๊ต์ฒด(๊ฐฑ์‹ )ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค. 
  • Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ํ•œ๋‹ค.

 

*๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ : ์ผ๋ฐ˜์ ์œผ๋กœ ์–ด๋– ํ•œ ํŠน์ • ๋ช…๋ น์–ด๊ฐ€ ์žˆ์œผ๋ฉด, ๊ทธ๊ฒƒ์ด ์™„๋ฃŒ๋œ ํ›„์— ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ, ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๋Š” ํŠน์ •์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ์ฝ”๋“œ๊ฐ€ ์™„๋ฃŒ๋˜๊ธฐ ์ „์— ๋‹ค์Œ์ฝ”๋“œ๋ฅผ ์ด์–ด์„œ ์ˆ˜ํ–‰ํ•˜๋Š”๊ฒƒ์„ ๋งํ•œ๋‹ค.

*Promise(ํ”„๋กœ๋ฏธ์Šค) :
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ํ™œ์šฉ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค. 

 

 

 


 

 

 

*Fetch ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ (1)

์•ต์ปค(<a>ํƒœ๊ทธ) ๋ฅผ ๊ฑธ์–ด๋†“๊ณ , ์ด๋ฅผ ํด๋ฆญํ•˜๊ฒŒ๋˜๋ฉด fetch๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์„œ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๋„๋ก ํ•˜๋Š” ์˜ˆ์ œ.

*ํŒŒ์ผ๊ตฌ์„ฑ
home.html
spring.txt

 

 

*Fetch ํ•จ์ˆ˜

function fetchPage(fileName){
  fetch(fileName).then(function(response){
    response.text().then(function(text){
      document.querySelector('#text').innerHTML = text;
    })})};

 

 

*spring.txt

 

*home.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <h1>fetch example</h1>
    <h2> season </h2>
<ol>
  <li> <a href="#!spring" onclick="fetchPage('spring.txt')">spring </a> </li>
  <li>summer</li>
  <li>fall</li>
  <li>winter</li>
</ol>
<p id="text"></p>
<script type="text/javascript">
function fetchPage(fileName){
  fetch(fileName).then(function(response){
    response.text().then(function(text){
      document.querySelector('#text').innerHTML = text;
    })})};
</script>
  </body>
</html>

 

*๊ฒฐ๊ณผํ™”๋ฉด

 

 

*Fetch ์˜ ๊ธฐ๋ณธ ์ฝ”๋“œ์™€ ์˜ต์…˜๋“ค

 

*๊ธฐ๋ณธ์ ์ธ fetch ์ฝ”๋“œ(์ถœ์ฒ˜ : mozilla)
fetch('http://example.com/movies.json')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
  });

๊ธฐ๋ณธ์ ์œผ๋กœ fetch๋Š” ํ•ด๋‹น url์˜ ์ •๋ณด๋ฅผ ์š”์ฒญํ•œ๋‹ค. 

 

Fetch์˜ ๋ชจ๋“  ์˜ต์…˜๋“ค
// Example POST method implementation:

postData('http://example.com/answer', {answer: 42})
  .then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
  .catch(error => console.error(error));

function postData(url = '', data = {}) {
  // Default options are marked with *
    return fetch(url, {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, *same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, *same-origin, omit
        headers: {
            'Content-Type': 'application/json',
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: 'follow', // manual, *follow, error
        referrer: 'no-referrer', // no-referrer, *client
        body: JSON.stringify(data), // body data type must match "Content-Type" header
    })
    .then(response => response.json()); // parses JSON response into native JavaScript objects 
}

 

'๐Ÿ’ป Web > -- HTTP | Network' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ฟ ํ‚ค์™€ ์„ธ์…˜(cookie session)  (0) 2020.05.10
RESTful API, ์›น์„œ๋น„์Šค(Web Service)  (0) 2020.05.10
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.