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

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


当前回答

使用Smart-IP.net Geo-IP API。例如,使用jQuery:

$(document).ready( function() {
    $.getJSON( "http://smart-ip.net/geoip-json?callback=?",
        function(data){
            alert( data.host);
        }
    );
});

其他回答

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)
  }
} );

对这个问题有两种解释。大多数人将“客户端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>

我非常喜欢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

实际上没有可靠的方法来获取客户端计算机的IP地址。

这涉及到一些可能性。如果用户有多个接口,那么使用Java的代码将会中断。

http://nanoagent.blogspot.com/2006/09/how-to-find-evaluate-remoteaddrclients.html

从这里的其他答案来看,听起来您可能想要获得客户端的公共IP地址,这可能是他们用来连接到互联网的路由器的地址。很多其他的答案都谈到了这个。我建议创建并托管您自己的服务器端页面,用于接收请求并使用IP地址响应,而不是依赖于其他人的服务,因为其他人的服务可能继续工作,也可能无法继续工作。