我需要一个
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
您可能正在寻找 Response.text():
fetch(url,...).then(response => response.text()).then(console.log)