react fetch给出一个空的响应体

Raz*_*lin 47 javascript fetch-api

我有一个react/redux应用程序,我正在尝试对服务器执行简单的GET请求:

fetch('http://example.com/api/node', {
  mode: "no-cors",
  method: "GET",
  headers: {
    "Accept": "application/json"
  }
}).then((response) => {
  console.log(response.body); // null
  return dispatch({
    type: "GET_CALL",
    response: response
  });
})
.catch(error => { console.log('request failed', error); });
Run Code Online (Sandbox Code Playgroud)

问题是.then()函数中的响应体是空的,我不知道为什么.我在网上检查了一些例子,看起来我的代码应该可行,所以我显然在这里遗漏了一些东西.问题是,如果我检查Chrome的开发工具中的网络选项卡,则会发出请求并收到我正在寻找的数据.

任何人都可以对这一个发光吗?

编辑:

我尝试转换响应.

使用.text():

fetch('http://example.com/api/node', {
  mode: "no-cors",
  method: "GET",
  headers: {
    "Accept": "application/json"
  }
})
.then(response => response.text())
.then((response) => {
  console.log(response); // returns empty string
  return dispatch({
    type: "GET_CALL",
    response: response
  });
})
.catch(error => { console.log('request failed', error); });
Run Code Online (Sandbox Code Playgroud)

并与.json():

fetch('http://example.com/api/node', {
  mode: "no-cors",
  method: "GET",
  headers: {
    "Accept": "application/json"
  }
})
.then(response => response.json())
.then((response) => {
  console.log(response.body);
  return dispatch({
    type: "GET_CALL",
    response: response.body
  });
})
.catch(error => { console.log('request failed', error); }); // Syntax error: unexpected end of input
Run Code Online (Sandbox Code Playgroud)

查看chrome开发工具:

在此输入图像描述

Rat*_*ica 107

我刚碰到这个.正如在这个答案中提到的,使用mode: "no-cors"会给你一个opaque response,它似乎不会返回正文中的数据.

opaque:对跨域资源的"no-cors"请求的响应. 严重受限制.

就我而言,我正在使用Express.在为Express安装cors并配置并删除后mode: "no-cors",我收到了一个承诺.响应数据将在承诺中,例如

fetch('http://example.com/api/node', {
  // mode: 'no-cors',
  method: 'GET',
  headers: {
    Accept: 'application/json',
  },
},
).then(response => {
  if (response.ok) {
    response.json().then(json => {
      console.log(json);
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,所需要做的就是将 `.then(json => {` 添加到我的 `response.json()` 语句的末尾。谢谢! (4认同)
  • 我正在使用 AWS API Gateway,在我的 lambda 中,我在响应中缺少此标头 ``"Access-Control-Allow-Origin": "*"```。添加它并删除“no-cors”对我有用。 (3认同)
  • 对于那些希望保留“no-cors”模式以尝试绕过某些 CORS 限制的人来说,不幸的是,无法解析从这些类型的请求返回的不透明响应。我非常困惑,因为我可以在浏览器开发工具中清楚地看到正确的响应,但它从未出现在我的代码中。有关该主题的更多讨论可以在这里找到:/sf/answers/3843450411/ (3认同)
  • @MikeKellogg 只是提醒一下,如果您想保持代码整洁,您可以`返回 response.json()` 并将 `.then(json=>` 部分放在最后一个括号和分号之间。`).then( json=>{});` (2认同)

Nai*_*han 21

在您可以访问之前,您需要转换responsejsonresponse.body

来自文档

fetch(url)
  .then(response => response.json())
  .then(json => {
    console.log('parsed json', json) // access json.body here
  })
Run Code Online (Sandbox Code Playgroud)


ope*_*onk 19

这需要更改前端 JS 和后端发送的标头。

前端

删除"mode":"no-cors"获取选项。

fetch(
  "http://example.com/api/docs", 
  {
    // mode: "no-cors",
    method: "GET"
  }
)
  .then(response => response.text())
  .then(data => console.log(data))
Run Code Online (Sandbox Code Playgroud)

后端

当您的服务器响应请求时,请包含指定请求来源的 CORS 标头。如果您不关心来源,请指定通配符*

原始响应应包含这样的标头。

Access-Control-Allow-Origin: *
Run Code Online (Sandbox Code Playgroud)


Flo*_*ent 5

您必须阅读响应的正文:

fetch(url)
  .then(res => res.text()) // Read the body as a string

fetch(url)
  .then(res => res.json()) // Read the body as JSON payload
Run Code Online (Sandbox Code Playgroud)

阅读身体后,您将可以对其进行操作:

fetch('http://example.com/api/node', {
  mode: "no-cors",
  method: "GET",
  headers: {
    "Accept": "application/json"
  }
})
  .then(response => response.json())
  .then(response => {
    return dispatch({
      type: "GET_CALL",
      response: response
    });
  })
Run Code Online (Sandbox Code Playgroud)

  • 仍然试图绕过这种“无心”模式。我也有类似情况。在这里使用各种解决方案,无取芯似乎成功了……没有x起源错误,并且在我的网络检查器中,我可以在“预览和响应”中查看数据。但是我似乎无法访问传递给我的'then'的响应对象的数据。json()text()似乎都没做多。不透明的响应是否意味着先获取数据然后将其清空,以便在完成后无法访问?:-/ (3认同)