如何将JSON传递给FetchAPI响应

我正在尝试创建ReadableStream对象,但不知道如何在其中传递json正文。

我应该创建一个{{3}}吗?如果是这样,我该怎么办?

我正在尝试使用它

const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(JSON.stringify({message: 'Test'}));
  },});

new Response(stream,{
  status: 304,statusText: 'Not Modified',headers: {
    'Content-Type': 'application/json;charset=utf-8',},});

但这似乎不起作用。

lorndrifter 回答:如何将JSON传递给FetchAPI响应

似乎我有点急于在这里提出一个问题。事实证明,这比我想象的要容易得多,不需要ReadableStream:

new Response(JSON.stringify({message: 'Test'}),{
  status: 418,statusText: 'Teapot',headers: {
    'Content-Type': 'application/json;charset=utf-8',},});

我的错误是我尝试使用null body status将主体设置为Response。

本文链接:https://www.f2er.com/3150767.html

大家都在问