json
这是创建 application/json
响应的快捷方式。它假设你使用的是 utf-8
编码。
¥This is a shortcut for creating application/json
responses. It assumes you are using utf-8
encoding.
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",
},
});
};
你还可以传递状态码和标头:
¥You can also pass a status code and headers:
export const loader = async () => {
return json(
{ not: "coffee" },
{
status: 418,
headers: {
"Cache-Control": "no-store",
},
}
);
};