如何使用javascript获取客户端IP地址和客户端计算机名称
问题描述:
Hello Everyone,
任何人都可以帮助我,
如何获取客户端IP地址和机器使用javascript或Jquery或任何其他。我用Google搜索了这么多,但我没有得到正确的结果。
我写了服务器端代码但是它给了我的服务器详细信息(Request.UserHostAddress和Dns.hostEntry)。
请帮帮我。
Hello Everyone,
Can anyone help me ,
how to get client Ip Address and Machine using javascript or Jquery or any another .I have Googled so much ,But i didn't get correct results.
I have written server side code but it is giving my server details( Request.UserHostAddress & Dns.hostEntry ).
help me Please .
答
<script type="text/javascript">
window.onload = function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
document.getElementsByTagName("head")[0].appendChild(script);
};
function DisplayIP(response) {
document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
}
</script>
</script>
您好,
没有直接支持从JavaScript获取机器的IP地址。但是一点点服务器端代码和AJAX你应该能够获得IP地址。所以写一个 WebMethod ,它将从Request对象中检索IP地址并返回它给来电者。对于AJAX,您可以使用非常出色的 JQuery 库。
问候,
Hello,
There is no direct support for obtaining the ip address of the machine from JavaScript. But little bit server side code and AJAX you should be able to obtain the Ip address. So write a WebMethod which will retrieve the IP Address from the Request object and return it to caller. For AJAX you can use the very excellent JQuery library.
Regards,
试试这个。
希望它对你有所帮助。
Try This.
Hope it will helpful for you.
string ip = Request.UserHostAddress;
string hostname = Request.UserHostName;