javascript javascript获取客户端ip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3540198/
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
javascript getting client ip
提问by JSNewbie
Hi I found how to get client ip by answer here: Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?
嗨,我在这里找到了如何通过回答获取客户端 IP: Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?
But I don't understand how to use it.
但我不明白如何使用它。
This is what I have:
这就是我所拥有的:
var user;
if ($('#user-id').length) {
user = $('#user-id').text();
} else {
http://jsonip.appspot.com/?callback=getip
function getip(json){
user = json.ip;
}
}
I don't understand how to include the url and how to use the getip function.
我不明白如何包含 url 以及如何使用 getip 函数。
I need to set the user to the ip address in the else.
我需要将用户设置为 else 中的 IP 地址。
Thanks!
谢谢!
回答by Kobi
Using jQuery, you could make a JSONPcall:
使用 jQuery,您可以进行JSONP调用:
$.getJSON('http://jsonip.appspot.com/?callback=?',
function(data){
alert(data.ip);
});
Probably easier to understand - an alternative, without jQuery, would be:
可能更容易理解 - 没有 jQuery 的替代方案是:
<script type="text/javascript">
function getip(data){
alert(data.ip);
}
</script>
<script type="text/javascript" src="http://jsonip.appspot.com/?callback=getip">
</script>
Note that when you include http://jsonip.appspot.com/?callback=getipas a script in your HTML you get valid JavaScript as response:
请注意,当您在 HTML 中包含http://jsonip.appspot.com/?callback=getip作为脚本时,您将获得有效的 JavaScript 作为响应:
getip({"ip": "147.234.2.5", "address":"147.234.2.5"});
This line executes your function with the proper parameter. Note that the function's name is given to it by query string.
此行使用正确的参数执行您的函数。请注意,函数的名称是由查询字符串指定的。
回答by Mohamed Mansour
You can only access your domain from JavaScript, you cannot do cross-domain communication with JavaScript unless you do the communication with iframes.
您只能从 JavaScript 访问您的域,除非您与iframes 进行通信,否则无法与 JavaScript 进行跨域通信。
If you have a dynamic backend such as PHP, Java, Python, Ruby, CGI, etc you can easily fetch the IP address from the user visiting your page. Each language has their own mechanism.
如果您有一个动态后端,例如 PHP、Java、Python、Ruby、CGI 等,您可以轻松地从访问您页面的用户那里获取 IP 地址。每种语言都有自己的机制。
- PHP: $_SERVER['REMOTE_ADDR']
- Python: socket.getaddrinfo
- Java: NetworkInterfaceclass
- Ruby: Socket.gethostname
- PHP: $_SERVER['REMOTE_ADDR']
- Python:socket.getaddrinfo
- Java的:NetworkInterface的类
- Ruby:Socket.gethostname
回答by Greg
You've got a random, free-floating URL in your javascript syntax.
您的 javascript 语法中有一个随机的、自由浮动的 URL。
This is how to make remote JSON request with jquery.
这是如何使用 jquery 进行远程 JSON 请求。
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
I would suggest you get more familiar with javascript in general.
我建议您更熟悉 javascript。
回答by Rishabh
You could use something like this:
你可以使用这样的东西:
<script type="text/javascript">
var userip;
</script>
...
<script type="text/javascript" src="http://l2.io/ip.js?var=userip"</script>
...
<script type="text/javascript">
document.write("Your IP is :", userip);
</script>
Here is the url of this library: http://l2.io/

