我想使用jQuery获取请求客户端的IP地址。而不是通过使用第三方API。在服务器端,我尝试了以下内容。Request.ServerVariables["REMOTE_ADDR"];
或
用于获取客户端请求ip地址的HttpContext.Current.Request.UserHostAddress,但希望使用jQuery在客户端获取该地址
发布于 2017-02-27 20:58:32
在您的服务器应用程序中,您必须创建一个操作,例如:
public ActionResult getip()
{
return Json(Request.UserHostAddress);
}并将其称为form 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?
否则,您必须使用thrid party API:
$.getJSON("http://jsonip.appspot.com?callback=?",
function(data){
alert( "Your ip: " + data.ip);
});https://stackoverflow.com/questions/42485215
复制相似问题