Fetch API

#JavaScript

whck6

Content-Type 會因為參數而改變。

const options = {
  method,
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json", // 指定
  },
  body: params, // 第二張圖
  // body: JSON.stringify(params), // 第一張圖
};

return fetch(url, options)
  .then((res) => {
    if (!res.ok) {
      throw new Error("請檢查網路連線");
    }

    return res.json();
  });

假如 params 沒有用 JSON.stringify 的話,實際傳出去的 Content-Type 不一定是 application/json .

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API