繁体   English   中英

无法从 Javascript net::ERR_CONNECTION_REFUSED 访问 localhost

[英]Cannot access localhost from Javascript net::ERR_CONNECTION_REFUSED

I have a node.js express server hosting my API on port 7070. I have a Flask server hosting my webpage on port 5000. I can access my API from the server using curl with curl http://localhost:7070/latest and the API returns结果成功。 在我的网络服务器上,我尝试使用 getJSON 调用 API,但它仅在我将 localhost 替换为服务器本地 IP 时才有效。

If I try to access my API using localhost with http://127.0.0.1:7070/latest , or http://localhost:7070/latest create a net::ERR_CONNECTION_REFUSED an error is thrown in chrome.

如果我将 localhost 更改为我的本地 IP 192.168.68.72 for http://192.168.68.72:7070/latest它可以正常工作。 我需要使用 localhost 因为我的应用程序是自托管的,所以 URL 不能硬链接。

我在这里做错了什么,我搜索了以前的问题并检查了端口是否已打开netstat -tnlp

在此处输入图像描述

是否有其他方式我需要说连接到同一台服务器但在不同的端口而不是本地主机上?

谢谢

感谢 Chris,我意识到 localhost 正在查看我自己的 PC,而不是服务器 PC。

为了解决这个问题,我使用了"//" + location.hostname + ":7070/latest"而不是http://localhost:7070/latest并且效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM