[英]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.