我需要以某种方式检索客户端的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/

其他回答

你可以使用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数据库。

声明:这个库是我写的

试试这个: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>

试试这个

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

小提琴

在你的页面中包含以下代码:<script type="text/javascript" src="http://l2.io/ip.js"></script>

点击这里了解更多