简体   繁体   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 the result successfully. 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结果成功。 On my webserver I am trying to call the API using getJSON however it only works when I replace localhost with the server local IP.在我的网络服务器上,我尝试使用 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. 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.

If I change localhost to my local IP 192.168.68.72 for http://192.168.68.72:7070/latest it works correctly.如果我将 localhost 更改为我的本地 IP 192.168.68.72 for http://192.168.68.72:7070/latest它可以正常工作。 I need to use localhost as my application is self-hosted so the URL can't be hard linked.我需要使用 localhost 因为我的应用程序是自托管的,所以 URL 不能硬链接。

What am I doing wrong here, I have searched previous issues and have checked that the port is open with netstat -tnlp我在这里做错了什么,我搜索了以前的问题并检查了端口是否已打开netstat -tnlp

在此处输入图像描述

Is there some other way I need to say connect to the same server but on a different port instead of localhost?是否有其他方式我需要说连接到同一台服务器但在不同的端口而不是本地主机上?

Thanks谢谢

Thanks to Chris I realised that localhost was looking at my own PC and not the server PC.感谢 Chris,我意识到 localhost 正在查看我自己的 PC,而不是服务器 PC。

To fix this I used "//" + location.hostname + ":7070/latest" instead of http://localhost:7070/latest and it worked perfectly.为了解决这个问题,我使用了"//" + location.hostname + ":7070/latest"而不是http://localhost:7070/latest并且效果很好。

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

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