Return JSON

In this example, an Edge Function is used to generate a JSON object, and the JSON object is accessed and previewed from a browser.

Code

const JSON_DATA = {
  content: 'Hello World', 
};

async function handleRequest() {
  return new Response(JSON.stringify(JSON_DATA), {
    headers: {
      'content-type': 'application/json; charset=UTF-8',
    },
  });
}

addEventListener('fetch', event => {
  event.respondWith(handleRequest());
});

Preview

Xnip2024-07-02_17-07-04.png

References