跳转到内容
搜索文档

返回 JSON

直接从 Worker 脚本返回 JSON,适用于构建 API 和中间件。 middleware.

最后更新 查看 MarkdownAgent 设置

如需快速上手,请点击下方按钮。

Deploy to Cloudflare

这会在你的 GitHub 账户中创建仓库,并将应用部署到 Cloudflare Workers。

export default {
  async fetch(request) {
    const data = {
      hello: "world",
    };

    return Response.json(data);
  },
};
export default {
	async fetch(request): Promise<Response> {
		const data = {
			hello: "world",
		};

		return Response.json(data);
	},
} satisfies ExportedHandler;
from workers import WorkerEntrypoint, Response
import json

class Default(WorkerEntrypoint):
    def fetch(self, request):
        data = json.dumps({"hello": "world"})
        headers = {"content-type": "application/json"}
        return Response(data, headers=headers)
use serde::{Deserialize, Serialize};
use worker::*;

#[derive(Deserialize, Serialize, Debug)]
struct Json {
    hello: String,
}

#[event(fetch)]
async fn fetch(_req: Request, _env: Env, _ctx: Context) -> Result<Response> {
    let data = Json {
        hello: String::from("world"),
    };
    Response::from_json(&data)
}
import { Hono } from "hono";

const app = new Hono();

app.get("*", (c) => {
	const data = {
		hello: "world",
	};

	return c.json(data);
});

export default app;

这篇文档对您有帮助吗?