【问题标题】:Get client IP address using jquery not iis address without using third party API [duplicate]使用jquery获取客户端IP地址而不是iis地址而不使用第三方API [重复]
【发布时间】:2017-07-18 00:50:04
【问题描述】:

我想使用 jQuery 获取请求客户端的 IP 地址。但不是通过使用第三方 API。 在服务器端,我尝试了以下操作。 Request.ServerVariables["REMOTE_ADDR"];

HttpContext.Current.Request.UserHostAddress 获取客户端请求ip地址 但想在客户端使用 jQuery

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    在您的服务器应用程序中,您必须创建一个操作,例如:

    public ActionResult getip()
    {
        return Json(Request.UserHostAddress);
    }
    

    并称它为 jQuery

    $.getJSON("http://yourhostname/controller/getip",
        function(ip){
           alert( "Your ip: " + ip);
    });
    

    但是,这只是一个简单的示例,它不涉及代理背后的客户端。检查这个答案:How can I get the client's IP address in ASP.NET MVC?

    否则你必须使用第三方 API:

     $.getJSON("http://jsonip.appspot.com?callback=?",
        function(data){
           alert( "Your ip: " + data.ip);
      });
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2013-03-19
      • 1970-01-01
      • 2012-12-05
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      相关资源
      最近更新 更多