我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
当前回答
你可以使用userinfo。IO javascript库。
<script type="text/javascript" src="userinfo.0.0.1.min.js"></script>
UserInfo.getInfo(function(data) {
alert(data.ip_address);
}, function(err) {
// Do something with the error
});
您还可以使用requirejs来加载脚本。
它将为您提供访问者的IP地址,以及有关其位置的一些数据(国家、城市等)。它基于maxmind geoip数据库。
声明:这个库是我写的
其他回答
好吧,我偏离了这个问题,但我今天有一个类似的需求,尽管我无法使用Javascript从客户端找到ID,但我做了以下工作。
服务器端:-
<div style="display:none;visibility:hidden" id="uip"><%= Request.UserHostAddress %></div>
使用Javascript
var ip = $get("uip").innerHTML;
我使用ASP。Net Ajax,但您可以使用getElementById而不是$get()。
发生的事情是,我在页面上有一个隐藏的div元素,用户的IP从服务器呈现。而在Javascript中,我只是加载这个值。
这可能对像你这样有类似需求的人(比如我,但我还没有弄清楚)有帮助。
干杯!
获取系统本地IP:
try {
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
var rtc = new RTCPeerConnection({ iceServers: [] });
if (1 || window.mozRTCPeerConnection) {
rtc.createDataChannel('', { reliable: false });
};
rtc.onicecandidate = function (evt) {
if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
};
rtc.createOffer(function (offerDesc) {
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
}, function (e) { console.warn("offer failed", e); });
var addrs = Object.create(null);
addrs["0.0.0.0"] = false;
function updateDisplay(newAddr) {
if (newAddr in addrs) return;
else addrs[newAddr] = true;
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
LgIpDynAdd = displayAddrs.join(" or perhaps ") || "n/a";
alert(LgIpDynAdd)
}
function grepSDP(sdp) {
var hosts = [];
sdp.split('\r\n').forEach(function (line) {
if (~line.indexOf("a=candidate")) {
var parts = line.split(' '),
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf("c=")) {
var parts = line.split(' '),
addr = parts[2];
alert(addr);
}
});
}
})();} catch (ex) { }
你可以使用userinfo。IO javascript库。
<script type="text/javascript" src="userinfo.0.0.1.min.js"></script>
UserInfo.getInfo(function(data) {
alert(data.ip_address);
}, function(err) {
// Do something with the error
});
您还可以使用requirejs来加载脚本。
它将为您提供访问者的IP地址,以及有关其位置的一些数据(国家、城市等)。它基于maxmind geoip数据库。
声明:这个库是我写的
Javascript / jQuery获取客户端的IP地址和位置(国家,城市)
你只需要嵌入一个带有“src”链接的标签到服务器。服务器将以Object / JSON的形式返回“codehelper_ip”,您可以立即使用它。
// First, embed this script in your head or at bottom of the page.
<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
// You can use it
<script language="Javascript">
alert(codehelper_ip.IP);
alert(codehelper_ip.Country);
</script>
更多信息在Javascript检测真实IP地址加国家
如果你正在使用jQUery,你可以尝试:
console.log(codehelper_ip);
它将显示关于返回对象的更多信息。
如果你想要回调函数,请试试这个:
// First, embed this script in your head or at bottom of the page.
<script language="Javascript" src="http://www.codehelper.io/api/ips/?callback=yourcallback"></script>
// You can use it
<script language="Javascript">
function yourcallback(json) {
alert(json.IP);
}
</script>
我非常喜欢api.ipify.org,因为它同时支持HTTP和HTTPS。
下面是一些使用jQuery获取IP的例子。
基于HTTPS的JSON格式
https://api.ipify.org?format=json
$.getJSON(“https://api.ipify.org/?format=json”, function(e) { 警报(e.ip); }); <script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script>
基于HTTP的JSON格式
http://api.ipify.org?format=json
$.getJSON(“http://api.ipify.org/?format=json”, function(e) { 警报(e.ip); }); <script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script>
基于HTTPS的文本格式
如果你不想要JSON格式,也有一个通过HTTPS的明文响应
https://api.ipify.org
基于HTTP的文本格式
在HTTP上也有明文响应
http://api.ipify.org