Javascript 获取作为html的post响应,但也包含状态

Javascript 获取作为html的post响应,但也包含状态,javascript,text,fetch,status,Javascript,Text,Fetch,Status,我有一个对API的获取调用。我需要状态和HTML响应 fetch('api/post/email'{ 方法:“POST”, 正文:JSON.stringify(数据) })。然后((响应)=>{ log(response.text()); console.log(response.body); 如果(response.status==200){ console.log(响应状态); }否则{ console.log(响应状态); } }); 我从上面得到这个结果: Promise {<p

我有一个对API的获取调用。我需要
状态
和HTML响应

fetch('api/post/email'{
方法:“POST”,
正文:JSON.stringify(数据)
})。然后((响应)=>{
log(response.text());
console.log(response.body);
如果(response.status==200){
console.log(响应状态);
}否则{
console.log(响应状态);
}
});
我从上面得到这个结果:

Promise {<pending>}
ReadableStream {locked: true}
404
Promise{}
ReadableStream{locked:true}
404
最后一个
status
很好,但是

使用fetch从API获取正文或HTML结果的最简单方法是什么?
fetch('api/post/email'{
方法:“POST”,
正文:JSON.stringify(数据)
})。然后((响应)=>{
response.text()。然后(re=>{
控制台日志(re);
});
console.log(response.body);
如果(response.status==200){
console.log(响应状态);
}否则{
console.log(响应状态);
}
});
fetch('api/post/email'{
方法:“POST”,
正文:JSON.stringify(数据)
})。然后((响应)=>{
response.text()。然后(re=>{
控制台日志(re);
});
console.log(response.body);
如果(response.status==200){
console.log(响应状态);
}否则{
console.log(响应状态);
}

});我能想到的最简单的方法是使用async/await

注意:
response.text()
返回解析为文本的承诺

fetch('api/post/email', {
  method: 'POST',
  body: JSON.stringify(data)
}).then(async (response) => {
  const text = await response.text();
  if (response.status == 200) {
    console.log(response.status);
  } else {
    console.log(response.status);
  }
  console.log(text);
});

我能想到的最简单的方法是使用async/await

注意:
response.text()
返回解析为文本的承诺

fetch('api/post/email', {
  method: 'POST',
  body: JSON.stringify(data)
}).then(async (response) => {
  const text = await response.text();
  if (response.status == 200) {
    console.log(response.status);
  } else {
    console.log(response.status);
  }
  console.log(text);
});