繁体   English   中英

如何使用 fetch().then() 获取响应体

[英]How to use fetch().then() to get Response body

我需要一个const来定义这个主体(字符串)。 然后我可以用它来做console.log()

fetch("url", {
            headers: {
                "Content-Type": "application/json",
                'Authorization': 'Basic ' + btoa(globalUsername + ":" + globalPassword),
            },
            method: "POST",
            body: moveBody
        }).then(response => console.log(response.status)).
        then(response => console.log(response.text(body)));

在此处输入图像描述

您可能正在寻找Response.text()

fetch(url,...).then(response => response.text()).then(console.log)

Promise.then可以链Promise.then参数是 object 从之前的Promise.then返回。

Response.text()返回字符串正文

Response.json()返回解析后的 json

fetch("url", {
    headers: {
      "Content-Type": "application/json",
      'Authorization': 'Basic ' + btoa(globalUsername + ":" + globalPassword),
    },
    method: "POST",
    body: moveBody
  })
  .then(response => console.log(response.status) || response) // output the status and return response
  .then(response => response.text()) // send response body to next then chain
  .then(body => console.log(body)) // you can use response body here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM