javascript - 以 html 格式获取帖子响应,但也获取状态
问题描述
我有一个对 API 的 fetch 调用。我需要两者status
和 HTML 响应。
fetch('api/post/email', {
method: 'POST',
body: JSON.stringify(data)
}).then((response) => {
console.log(response.text());
console.log(response.body);
if (response.status == 200) {
console.log(response.status);
} else {
console.log(response.status);
}
});
我从上面得到这个结果:
Promise {<pending>}
ReadableStream {locked: true}
404
最后一个status
很好,但是...
使用 fetch 从 API 获取正文或 HTML 结果的最简单方法是什么?
解决方案
fetch('api/post/email', {
method: 'POST',
body: JSON.stringify(data)
}).then((response) => {
response.text().then(re => {
console.log(re);
});
console.log(response.body);
if (response.status == 200) {
console.log(response.status);
} else {
console.log(response.status);
}
});
Response.text() 返回一个承诺,因此您必须像处理它一样处理它。(传入回调)之后,您可以在回调中记录它。
推荐阅读
- python-3.x - 在具有可变格式的熊猫中分离数据
- node.js - 在 TypeScript 中使用 koa-views
- python - 修改 pandas MultiIndex 的子集
- docker - 找不到 docker build 所需的文件
- python - 如何获取数据框列中列表之间的共同值?
- jquery - jQuery无法运行多次单击按钮
- android - X509TrustManager 是否在每个 Android 应用程序会话中只检查一次 API 请求?
- tensorflow - 如何在 GPU 上沿着深度学习模型进行 cv2 操作?
- c# - 将数字参考字符转换为实际字符
- python - 提高数据集生成中的代码效率