// 設定表頭如此
  Content-Type: application/x-www-form-urlencoded;charset=utf-8
  // 使用 GET 方法 將參數直接寫在待入的 url 上
  url/?title=test&name=Tom
  // 使用 POST 方法 則將資料轉成字串傳遞
  const data = 'name=TOM&age=30'
  xhr.send(data)
  // PUT
  data = { name:'TEST' } name 以外的屬性刪除
  // PATCH
  data = { name:'TEST' } title 保留,只更新 name
  const xhr = new XMLHttpRequest()
  // true 表 async 非同步(預設) false 表 sync 同步
  xhr.open('GET', 'http://localhost:3000/users', true)
  
  // 資料傳送成功並接收回應後,要作些甚麼動作
  xhr.onload = function () {
    if (xhr.status >= 200 && xhr.status < 400) {
      // 回傳值為字串 需要先轉換成 JSON 再作運算
      console.table(JSON.parse(this.response)) 
    }
  }
  // GET 不需要傳送資料
  xhr.send(null) 
  const data = { name: 'TEST2', title: 'Title2' }
  const xhr = new XMLHttpRequest()
  
  // PUT、PATCH、DELETE (皆需指定id) ~/users/1
  xhr.open('POST', 'http://localhost:3000/users', true)
  
  // 設定表頭
  xhr.setRequestHeader('Content-type', 'application/json ')
  
  xhr.onload = function () {
    if (xhr.status >= 200 && xhr.status < 400) { 
      // 回傳剛剛送出的結果 回傳值為字串需先轉換成 JSON
      console.table(JSON.parse(this.response)) 
    }
  }
  
  // 需將資料轉成 String
  xhr.send(JSON.stringify(data))
  
  // 如果未轉成 String 資料格式不符合時會噴 400 Bad Request
  xhr.send(data) // Ex: ['test']
credentials: 'include'
fetch('http://localhost:3000/users/2')
  .then(res => res.json()) // 先將其轉成 JSON 檔
  .then((data) => { console.table(data) })
TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body
  // PUT、PATCH、DELETE (皆需指定id) ~/users/1
  const postUrl = 'http://localhost:3000/users'
  const newData = { name: 'PATCH', title: 'PATCH Title' }
  function postData(url, data = null) {
    return fetch(url, {
      method: 'POST',
      credentials: 'include', // 需傳送 Cookie 必須開啟
      // header外參數 cache mode redirect referrer
      headers: {
        'Content-Type': 'application/json',
      },
      // 注意若 Method 為 GET 不可有 body 會噴錯
      body: JSON.stringify(data),
      // 回傳仍是 Promise物件
    }).then((res) => {
    
      // Response objects
      console.dir(res)
      
      // 狀態是否為 200-299
      if (res.ok) { res.json() }
      
      // 即使不設定,他也會自動噴出錯誤訊息
      throw new Error('Network response was not ok.')
    })
  }
  postData(postUrl, newData).then(res => res.json())
    .then((data) => { console.table(data) })
    .catch((err) => { console.error(err) })
    credentials: 'include' (皆發送)
    credentials: 'same-origin' (同源)
    credentials: 'omit' (預設值,不發送Cookie)
  // 有 get / set / has / delete 方法可用
  const myHeaders = new Headers({
    'Content-Type': 'application/json',
  })
  const myInit = {
    method: 'GET',
    headers: myHeaders,
    mode: 'cors',
    cache: 'default', 
  }
  const myRequest = new Request('url', myInit)
  fetch(myRequest)
  // blob 圖檔範例
  const myImage = document.querySelector('.TargetImg')
  fetch('flowers.jpg')
    .then(res => res.blob())
    .then((myBlob) => { 
      let objectURL = URL.createObjectURL(myBlob) 
      myImage.src = objectURL 
    })
  let formData = new FormData()
  let fileField = document.querySelector("input[type='file']")
  formData.append('username', 'abc123')
  formData.append('avatar', fileField.files[0])
  fetch('https://example.com/profile/avatar', {
    method: 'PUT',
    body: formData,
  })
    .then(res => res.json())
    .catch(err => console.error('Error:', err))
    .then(res => console.log('Success:', res))
    run()
    async function run() {
      const getUrl = 'http://localhost:3000/users'
      const postUrl = 'http://localhost:3000/users'
      const newData = { name: 'test' }
      const post = await postData(postUrl, newData) // 回傳Promise物件
      const res = await fetch(getUrl)
      const data = await res.json()
      console.table(data)
    }
    function postData(url, data = null) {
      return fetch(url, {
        method: 'POST',
        credentials: 'include', // 需傳送 Cookie 必須開啟
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
      })
    }