javascript 在没有服务器端语言的情况下获取 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11011058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an IP address without a server side language
提问by HakimuddinHussain
I am trying to make a simple website with only HTML & JavaScript. I want to display the visitors IP address on the screen. I tried to search a lot and came to a conclusion that that is not possible through JavaScript and I would have to use some server side scripting language like PHP. However, I don't want to increase the overheads, how can I achieve this without server side programming?
我正在尝试使用 HTML 和 JavaScript 制作一个简单的网站。我想在屏幕上显示访问者的 IP 地址。我尝试了很多搜索,得出的结论是,通过 JavaScript 是不可能的,我必须使用一些服务器端脚本语言,如 PHP。但是,我不想增加开销,如何在没有服务器端编程的情况下实现这一目标?
回答by Ja?ck
It's easy, just find a service that will return the IP to you via JSONP:
很简单,只要找到一个可以通过 JSONP 将 IP 返回给你的服务:
How to get client IP address using jQuery
Other resources:
其他资源:
Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?
我可以使用客户端 Javascript 执行 DNS 查找(主机名到 IP 地址)吗?
回答by Ken Le
回答by Quentin
You can't. JavaScript has no access to any API that will give that information other than via an HTTP request.
你不能。除了通过 HTTP 请求之外,JavaScript 无法访问任何会提供该信息的 API。