React Router v7 已發布。 查看文件
json

json

這是建立 application/json 回應的快捷方式。它假設您使用 utf-8 編碼。

import { json } from "@remix-run/node"; // or cloudflare/deno

export const loader = async () => {
  // So you can write this:
  return json({ any: "thing" });

  // Instead of this:
  return new Response(JSON.stringify({ any: "thing" }), {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
    },
  });
};

您也可以傳遞狀態碼和標頭

export const loader = async () => {
  return json(
    { not: "coffee" },
    {
      status: 418,
      headers: {
        "Cache-Control": "no-store",
      },
    }
  );
};
文件和範例授權於 MIT