LoginSignup
52
54

More than 5 years have passed since last update.

久しぶりにJavaScriptでHTTPリクエスト処理を書こうとしたら、もはや別言語だった件

Posted at
function main() {
  (async () => {
    try {
      const response = await fetch('/cms/api/csv/upload', createFetchPostData());
      const json = await response.json();
      console.log(json);
    } catch (error) {
      console.log(error);
    }
  })();
}

function createFetchPostData(data = {}) {
  return {
    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; charset=utf-8',
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrer: 'no-referrer', // no-referrer, *client
    body: JSON.stringify(data), // 本文のデータ型は 'Content-Type' ヘッダーと一致する必要があります
  };
}

main() // 実行!!

便利なんだろうけど、アロー演算子とかスコープが変わったりするし、うーむ。って感じ。

Bootstrapも5からjQuery無くすっていうし、ネイティブJSがトレンドかなぁと思ったり。

52
54
13

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
52
54