Get client IP address via third party web service

后端 未结 7 1453
一整个雨季
一整个雨季 2020-11-27 19:02

I would like to read my ip address from the following page(http://l2.io/ip or other) using javascript to save him in my variable \"myIp\".

function getMyIP()         


        
7条回答
  •  有刺的猬
    2020-11-27 19:24

    If you face an issue of CORS, you can use https://api.ipify.org/.

    function httpGet(theUrl)
    {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open( "GET", theUrl, false );
        xmlHttp.send( null );
        return xmlHttp.responseText;
    }
    
    
    publicIp = httpGet("https://api.ipify.org/");
    alert("Public IP: " + publicIp);
    

    I agree that using synchronous HTTP call is not good idea. You can use async ajax call then.

提交回复
热议问题