我需要一个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)));
最佳答案
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/