Return JSON
Ce contenu n’est pas encore disponible dans votre langue.
Return a Simple JSON Data.
async function onRequest({ req }) {
const request = req.request;
// Only accept GET Requests
if (request.method !== "GET") return new Response("Not Allowed", {
status: 405,
headers: {
Allow: "GET"
}
});
const data = {
hello: "world",
};
const json = JSON.stringify(data, null, 2);
// Send the response back
req.respondWith(new Response(json, {
status: 200,
headers: {
"content-type": "application/json;charset=UTF-8",
}
}))
}
export { onRequest }