在不使用第三方API的情况下,使用jquery not iis地址获取客户端IP地址

时间:2017-02-27 11:56:51

标签: asp.net-mvc

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

OR

HttpContext.Current.Request.UserHostAddress获取客户端请求的IP地址  但希望使用jQuery在客户端获取它

1 个答案:

答案 0 :(得分: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);
  });
相关问题