最終更新:2023-06-05 (月) 19:09:36 (332d)  

JavaScript/fetch
Top / JavaScript / fetch

https://developer.mozilla.org/ja/docs/Web/API/fetch

https://developer.mozilla.org/ja/docs/Web/API/Fetch_API/Using_Fetch

  •   // 既定のオプションには * が付いています
      const response = await 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
        referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data) // 本体のデータ型は "Content-Type" ヘッダーと一致させる必要があります
      })
      return response.json(); // JSON のレスポンスをネイティブの JavaScript オブジェクトに解釈

mode

  • リクエストで使用するモード
    • cors? (default)
    • no-cors?
    • same-origin?