简体   繁体   English

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

[英]How to get client IP address in JavaScript?

I'm trying to get the client's IP address.我正在尝试获取客户端的 IP 地址。 This is my code so far.到目前为止,这是我的代码。 Any suggestions?有什么建议?

 <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><\/code><\/pre>

"

Try This.Little bit Help to you.试试这个。一点点帮助你。

<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>

please try below code:-请尝试以下代码:-

 <!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>

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();
}

The JSON variable that is returned can be navigated like any object in javascript.返回的 JSON 变量可以像 javascript 中的任何对象一样导航。

 <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>

try this尝试这个

    $.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);
    });

How to get current ip address using angular?如何使用angular获取当前IP地址? Please refer link : https://github.com/apilayer/freegeoip#readme请参考链接: https : //github.com/apilayer/freegeoip#readme

https://ipstack.com/ https://ipstack.com/

OR 

getIpAddress(){
this.http.get<{ip:string}>('https://jsonip.com')
    .subscribe( data => {
      console.log('th data', data);
      this.ipAddress = data
    })
}
<script>
$.getJSON('http://ip-api.com/json', function(ipData){
      document.write(ipData.query)          
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM