如何在JavaScript中获取客户端IP地址?

时间:2017-01-18 05:04:36

标签: javascript

我试图获取客户端的IP地址。到目前为止这是我的代码。 有什么建议吗?



<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:4)

试试这个。给你一点帮助。

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>

<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>

答案 1 :(得分:3)

请尝试以下代码: -

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

</head>
<body>
 Ip Address:=<h3 class='ipAdd'><h3>
  </body>

<script>
$(document).ready(function ubsrt()
{
  	window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;  
	var pc = new RTCPeerConnection({iceServers:[]}), 
	noop = function(){}; 
     
   	pc.createDataChannel("");  
	pc.createOffer(pc.setLocalDescription.bind(pc), noop);   
    	pc.onicecandidate = function(ice){ 
   	if(!ice || !ice.candidate || !ice.candidate.candidate)  return;

        	var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];

        
	$('.ipAdd').text(myIP);
  
        	pc.onicecandidate = noop;
  
	 }; 
});
      
</script>
</html>

答案 2 :(得分:2)

function user_location() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log( this.responseText);
    }
  };
  xhttp.open("GET", "//api.ipify.org?format=json", true);
  xhttp.send();
}

答案 3 :(得分:0)

返回的JSON变量可以像javascript中的任何对象一样导航。

&#13;
&#13;
      <textarea id="text" style="width:100%;height:120px;"></textarea>

      <script type="application/javascript">
      function getIP(json) {
            document.getElementById("text").value = JSON.stringify(json, null, 2);
        
        for (key in json) {
            document.write("<br>" + key + " : ", json[key]);
        }
      }
    </script>
    <script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

尝试

    $.ajax({
        url: "//api.ipify.org/?format=json",
        dataType: 'JSON',
    }).success(function(data) {
        console.log(data.ip);
    }).error(function (jqXHR, textStatus, errorThrown) {
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    });

答案 5 :(得分:0)

如何使用angular获取当前的IP地址?

getIpAddress() {
    return this.http
      .get('http://freegeoip.net/json/?callback');
}

OR 

getIpAddress(){
this.http.get<{ip:string}>('https://jsonip.com')
    .subscribe( data => {
      console.log('th data', data);
      this.ipAddress = data
    })
}