gpt4 book ai didi

javascript - 如何使用 fetch().then() 获取响应正文

转载 作者:行者123 更新时间:2023-12-03 08:01:43 27 4
gpt4 key购买 nike

我需要一个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)));

enter image description here

最佳答案

Promise.then可以链接Promise.then参数是从先前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

关于javascript - 如何使用 fetch().then() 获取响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73855468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com