无法在本地主机(Amazon Cloud 9)中获取我的本地 API 运行

Failing to fetch my local API running in localhost (Amazon Cloud 9)

我有一个简单的 flask 服务器,当它接收到 return 一个 JSON(当你 return 一个 python dict 时,flask 会自动这样做)当它收到一个 GET 请求 / 端点。

在我的 5000 端口上 运行:

我知道它是 运行 并且可以访问,因为我设法请求它,并收到有效响应,使用 curl,两次:

这两个请求也会在第二次打印时记录到服务器日志中。

我正在尝试从 html/js 脚本 fetch 到我的服务器:

const URL = "http://127.0.0.1:5000/"
fetch( URL )
.then(response=>response.json())
.then(json=>console.log(json))

const URL = "http://localhost:5000/"
fetch( URL )
.then(response=>response.json())
.then(json=>console.log(json))

但是我得到了两次同样的错误:

1: GET http://localhost:5000/ net::ERR_CONNECTION_REFUSED

2:Uncaught (in promise) TypeError: Failed to fetch

我知道代码本身是有效的,因为我设法获取了 Githubs API:

const URL = "https://api.github.com/users/nluizsoliveira"
fetch( URL )
.then(response=>response.json())
.then(json=>console.log(json))

我不确定为什么我无法获取到我的本地主机。我认为这与 cloud9 如何处理端口有关。从 url 中删除 http://localhost 时:

const URL = ":5000"
fetch( URL )
.then(response=>response.json())
.then(json=>console.log(json))

代码片段也失败了,但似乎请求以某种方式将 url 附加到我的 C9 url。

有人遇到过这种情况吗? 提前致谢!


编辑: 澄清一下,我不是 运行 而是 js/html(直接)在我的浏览器选项卡上。我在 C9 的内置浏览器上 运行 它,可通过“预览 运行 应用程序”获得:

借助 AWS Cloud 9 预览版,AWS 为您提供私有 link,例如 https://12a34567b8cd9012345ef67abcd890e1.vfs.cloud9.us-east-2.amazonaws.com,让您可以访问您的应用程序。要获得该 link 单击预览应用程序并从浏览器选项卡中复制它。

在您的代码中使用 link 而不是 localhost。 AWS Documentation:Preview a running application.