如何在 JavaScript 中检查另一台服务器是否已启动?
在我的网络应用程序中,我需要检查另一台服务器(在不同的 IP 地址上)是否已启动。我知道由于跨域限制,我不能使用直接的 ajax 请求。我无法使用 JSONP,因为我对其他服务器没有任何控制权。我还需要反复检查服务器是否启动,因为它有时可能不可用。所有这些都不容易。
然而,由于我只关心对远程服务器的请求是否成功(并且我不关心服务器可能返回的任何数据),所以我希望这是可能的。
我无法在服务器端执行此操作,因为服务器端可能对其他服务器可见,也可能不可见,我真正关心的是客户端(Web 浏览器)具有可见性。
我可以将一个新的 iframe 附加到 src 等于远程服务器地址的页面,但是如何检查 iframe 内容何时已成功加载?另外我如何提出定期请求?通过重复创建新的 iframe?这看起来很不干净。
有什么方法可以干净可靠地做到这一点吗?
In my webapp I need to check if another server (on a different ip address) is up. I know I can't use direct ajax requests for this because of cross domain restrictions. I cannot use JSONP because I don't have any control over the other server. I also need to repeatedly check for the server being up as it may be unavailable some of the time. All these don't make it easy.
However since I only care about a request to the remote server succeeding (and I don't care about any data that the server might return), I was hoping that it would be possible.
I can't do this on the server side because the server side may or may not have visibility to the other server, what I really care about is that the client (web browser) have visibility.
I can append a new iframe to the page with the src equal to the remote server address but then how can I check when (IF) an iframe contents have been loaded successfully? Also how do I make periodic requests? By creating new iframes repeatedly? That seems quite unclean.
Is there any way to do this cleanly and reliably?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要从远程服务器加载图像,然后检查图像高度或其他内容是否符合您的预期。
如果您无法将图像放置在远程服务器上,则需要使用 CORS
You want to load an Image from a remote server then check that the image height or whatever is what you expect it is.
If you can't place an image on the remote server then you will need to use CORS
不正确。请参阅 JSONP
Not true. See JSONP
使用 jQuery 与 Raynos 类似的解决方案:
A similar solution to Raynos' using jQuery: