メモ置き場

Fetch APIメモ

jsonの読み込み

fetch(url)
  .then(resp => resp.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));

テキストの読み込み

fetch(url)
  .then(resp => resp.text())
  .then(text => console.log(text))
  .catch(err => console.error(err));

Cookie付きのリクエスト

fetch(url, {
  credentials: "include"
})

jsonをPOSTする

fetch(url,{
  method: 'POST',
  body: JSON.stringify(data),
  headers:{
    'Content-Type': 'application/json'
  }
})

参照