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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:48:38  来源:igfitidea点击:

Getting an IP address without a server side language

javascriptip

提问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 Ken Le

NO. Javascript, can't access IP Address. You need an API or a PHP/ASP/JSON File on your web Server. Try this service

不。Javascript,无法访问 IP 地址。您的 Web 服务器上需要 API 或 PHP/ASP/JSON 文件。试试这个服务

<script src="http://www.codehelper.io/api/ips/?js"></script>
<script>
alert(codehelper_ip.IP);
</script>

回答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。