我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。

然而,我并不反对使用免费的第三方脚本/服务。


当前回答

对这个问题有两种解释。大多数人将“客户端IP”解释为Web服务器在局域网外和Internet上看到的公共IP地址。不过,在大多数情况下,这不是客户端计算机的IP地址

我需要运行我的JavaScript软件的浏览器的计算机的真实IP地址(它几乎总是LAN上NAT层后面的本地IP地址)。

Mido发布了一个奇妙的答案,上面,这似乎是唯一的答案,真正提供了客户端的IP地址。

谢谢你,Mido!

但是,给出的函数是异步运行的。我需要在代码中实际使用IP地址,使用异步解决方案时,我可能会尝试在检索/学习/存储IP地址之前使用IP地址。在使用它们之前,我必须能够等待结果的到来。

下面是Mido函数的“可等待”版本。我希望它能帮助到其他人:

function findIP(onNewIP) { // onNewIp - your listener function for new IPs var promise = new Promise(function (resolve, reject) { try { var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome var pc = new myPeerConnection({ iceServers: [] }), noop = function () { }, localIPs = {}, ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g, key; function ipIterate(ip) { if (!localIPs[ip]) onNewIP(ip); localIPs[ip] = true; } pc.createDataChannel(""); //create a bogus data channel pc.createOffer(function (sdp) { sdp.sdp.split('\n').forEach(function (line) { if (line.indexOf('candidate') < 0) return; line.match(ipRegex).forEach(ipIterate); }); pc.setLocalDescription(sdp, noop, noop); }, noop); // create offer and set local description pc.onicecandidate = function (ice) { //listen for candidate events if (ice && ice.candidate && ice.candidate.candidate && ice.candidate.candidate.match(ipRegex)) { ice.candidate.candidate.match(ipRegex).forEach(ipIterate); } resolve("FindIPsDone"); return; }; } catch (ex) { reject(Error(ex)); } });// New Promise(...{ ... }); return promise; }; //This is the callback that gets run for each IP address found function foundNewIP(ip) { if (typeof window.ipAddress === 'undefined') { window.ipAddress = ip; } else { window.ipAddress += " - " + ip; } } //This is How to use the Waitable findIP function, and react to the //results arriving var ipWaitObject = findIP(foundNewIP); // Puts found IP(s) in window.ipAddress ipWaitObject.then( function (result) { alert ("IP(s) Found. Result: '" + result + "'. You can use them now: " + window.ipAddress) }, function (err) { alert ("IP(s) NOT Found. FAILED! " + err) } ); <h1>Demo "Waitable" Client IP Retrieval using WebRTC </h1>

其他回答

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

有一种更简单和免费的方法,不需要访问者的任何许可。

它包括向http://freegeoip.net/json提交一个非常简单的Ajax POST请求。一旦收到JSON格式的位置信息,您就可以通过更新页面或重定向到新页面来做出相应的反应。

以下是提交位置信息请求的方法:

jQuery.ajax( { 
  url: '//freegeoip.net/json/', 
  type: 'POST', 
  dataType: 'jsonp',
  success: function(location) {
     console.log(location)
  }
} );

您可以使用我的服务http://ipinfo.io,它将为您提供客户端IP、主机名、地理位置信息和网络所有者。下面是一个记录IP的简单示例:

$.get("http://ipinfo.io", function(response) {
    console.log(response.ip);
}, "jsonp");

下面是一个更详细的JSFiddle示例,它还输出了完整的响应信息,因此您可以看到所有可用的详细信息:http://jsfiddle.net/zK5FN/2/

试试这个

$.get("http://ipinfo.io", function(response) {
    alert(response.ip);
}, "jsonp");

OR

$(document).ready(function () {
    $.getJSON("http://jsonip.com/?callback=?", function (data) {
        console.log(data);
        alert(data.ip);
    });
});

小提琴

通常不可能,除非使用某种外部服务。