我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
当前回答
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>
其他回答
用Java脚本查找IP
为了获得IP地址,我正在对免费Web服务进行JSON调用。就像
[jsonip.com/json, ipinfo。Io /json, www.telize.com/geoip, ip-api.com/json, api.hostip.info/get_json.php]
并且我正在传递回调函数的名称,它将在请求完成时被调用。
<script type="text/javascript">
window.onload = function () {
var webService = "http://www.telize.com/geoip";
var script = document.createElement("script");
script.type = "text/javascript";
script.src = webService+"?callback=MyIP";
document.getElementsByTagName("head")[0].appendChild(script);
};
function MyIP(response) {
document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
}
</script>
<body>
<form>
<span id = "ipaddress"></span>
</form>
</body>
对于XML响应代码
WebRTC不需要服务器支持。
Appspot.com回调服务不可用。ipinfo。IO似乎在工作。
我做了一个额外的步骤,检索所有的地理信息使用AngularJS。(感谢里卡多)来看看。
<div ng-controller="geoCtrl">
<p ng-bind="ip"></p>
<p ng-bind="hostname"></p>
<p ng-bind="loc"></p>
<p ng-bind="org"></p>
<p ng-bind="city"></p>
<p ng-bind="region"></p>
<p ng-bind="country"></p>
<p ng-bind="phone"></p>
</div>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
<script src="http://code.angularjs.org/1.2.12/angular-route.min.js"></script>
<script>
'use strict';
var geo = angular.module('geo', [])
.controller('geoCtrl', ['$scope', '$http', function($scope, $http) {
$http.jsonp('http://ipinfo.io/?callback=JSON_CALLBACK')
.success(function(data) {
$scope.ip = data.ip;
$scope.hostname = data.hostname;
$scope.loc = data.loc; //Latitude and Longitude
$scope.org = data.org; //organization
$scope.city = data.city;
$scope.region = data.region; //state
$scope.country = data.country;
$scope.phone = data.phone; //city area code
});
}]);
</script>
工作页面在这里:http://www.orangecountyseomarketing.com/projects/_ip_angularjs.html
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>
首先是实际的答案:不可能仅使用客户端执行的代码来找出您自己的IP地址。
但是,您可以对https://hutils.loxal.net/whois执行GET请求,并接收类似这样的内容以获取客户端的IP地址
{
"ip": "88.217.152.15",
"city": "Munich",
"isp": "M-net Telekommunikations GmbH",
"country": "Germany",
"countryIso": "DE",
"postalCode": "80469",
"subdivisionIso": "BY",
"timeZone": "Europe/Berlin",
"cityGeonameId": 2867714,
"countryGeonameId": 2921044,
"subdivisionGeonameId": 2951839,
"ispId": 8767,
"latitude": 48.1299,
"longitude": 11.5732,
"fingerprint": "61c5880ee234d66bded68be14c0f44236f024cc12efb6db56e4031795f5dc4c4",
"session": "69c2c032a88fcd5e9d02d0dd6a5080e27d5aafc374a06e51a86fec101508dfd3",
"fraud": 0.024,
"tor": false
}
使用Smart-IP.net Geo-IP API。例如,使用jQuery:
$(document).ready( function() {
$.getJSON( "http://smart-ip.net/geoip-json?callback=?",
function(data){
alert( data.host);
}
);
});