我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
当前回答
试试这个:http://httpbin.org/ip(或https://httpbin.org/ip)
https的示例:
$.getJSON('https://httpbin.org/ip', function(data) {
console.log(data['origin']);
});
来源:http://httpbin.org/
其他回答
<!DOCTYPE html>
<html ng-app="getIp">
<body>
<div ng-controller="getIpCtrl">
<div ng-bind="ip"></div>
</div>
<!-- Javascript for load faster
================================================== -->
<script src="lib/jquery/jquery.js"></script>
<script src="lib/angular/angular.min.js"></script>
<script>
/// Scripts app
'use strict';
/* App Module */
var getIp = angular.module('getIp', [ ]);
getIp.controller('getIpCtrl', ['$scope', '$http',
function($scope, $http) {
$http.jsonp('http://jsonip.appspot.com/?callback=JSON_CALLBACK')
.success(function(data) {
$scope.ip = data.ip;
});
}]);
</script>
</body>
</html>
试试这个:http://httpbin.org/ip(或https://httpbin.org/ip)
https的示例:
$.getJSON('https://httpbin.org/ip', function(data) {
console.log(data['origin']);
});
来源:http://httpbin.org/
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>
您可以通过服务器端与JSONP进行中继
而谷歌找到一个,发现它在这里所以我可以执行一个DNS查找(主机名到IP地址)使用客户端Javascript?
<script type="application/javascript">
function getip(json){
alert(json.ip); // alerts the ip address
}
</script>
<script type="application/javascript" src="http://www.telize.com/jsonip?callback=getip"></script>
注意:telize.com API已于2015年11月15日永久关闭。
好吧,我偏离了这个问题,但我今天有一个类似的需求,尽管我无法使用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中,我只是加载这个值。
这可能对像你这样有类似需求的人(比如我,但我还没有弄清楚)有帮助。
干杯!