javascript - 为什么 fetching(API) 给我未定义?
问题描述
我使用 Flask 创建了一个 API,在 Postman 中使用它时它工作得很好,给了我 JSON。无论如何,当我尝试在 javascript 中获取它时,它给了我未定义:
api = 'http://127.0.0.1:5000/';
const getData = () => {
fetch(api)
.then(response => {
response.json();
})
.then(data => {
console.log(data);
});
};
getData();
正如我所说,当我尝试记录数据时,它会打印
undefined
解决方案
您需要返回您的 json 数据(return response.json();
),固定片段:
api = 'http://127.0.0.1:5000/';
const getData = () => {
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
});
};
getData();
推荐阅读
- ssis - 派生列的问题
- recharts - Recharts 放大挂钩
- c++ - 我需要帮助将此 ppc 指令翻译成 c++
- c# - 自定义 Asp.NET Core Identity 未经授权的响应类型和状态码
- python - 在 pynput 的鼠标监听器中使用 while 循环
- php - CSP 框架祖先 - 如何阻止除一页之外的整个域?
- java - 代码在 main 方法中工作,但不在 Apache 上运行的 Spring 中
- xaml - 我在 Xamarin Forms App.Shell 中的颜色配置缺少什么?
- javascript - 设置 HTML 表格内容而不是 Ajax
- node.js - node-postgres:数据库“database_name”不存在错误