首页 > 解决方案 > 如何使用 JavaScript 或 jQuery 在 cmd 中获取我们在 ipconfig 中获得的本地系统 IP?

问题描述

我已经使用下面的代码来获取系统本地 ip 但没有得到它,

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script>
    function callback(response)
    {
        
        console.log(response);
        document.getElementById("data").innerHTML=response.IPv4;
    }
    $.ajax({
        
    url:"https://geoip-db.com/jsonp/",
    dataType:"jsonp"
    })

</script

我也使用了这段代码

    <script type="text/javascript">
    
         var ip='<%=request.getRemoteHost()%>';
         document.getElementById('list1').innerHTML = ip;
         
         
         var ip1='<%=request.getRemoteAddr()%>';
         document.getElementById('list2').innerHTML = ip1;
         
         var ip2='<%=request.getLocalAddr()%>';
         document.getElementById('list3').innerHTML = ip2;
    
    </script> 



  I have also used the below code but I am not getting the IP that we get from inconfig from the command prompt
    
<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];

            console.log('my IP: ', myIP); 
              $('.ipAdd').text(myIP);
          alert("IP: "+myIP);
            pc.onicecandidate = noop;
  
     }; 
});
</script> 

我想要使​​用 javascript 与 ipconfig 相同的 IP。我使用了很多代码来查找 IP,但这不是我从 IPCONFIg 获得的 IP

标签: javascript

解决方案


用这个:

function callback(response) {
  console.log(response);
  document.getElementById("data").innerHTML = response.IPv4;
}

$.ajax({
  url: "https://geoip-db.com/jsonp/",
  dataType: "jsonp"
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="data"></p>


推荐阅读