ipinfo.io のJSONP APIを試してみた

こちらのエントリー で紹介されている ipinfo.io のJSONPを使った検索サンプルです

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Geo Sample</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
    <style type="text/css">
      body {
        padding-top: 50px;
      }
      .starter-template {
        padding: 40px 15px;
        text-align: center;
      }
    </style>
  </head>

  <body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false"
          aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active">
              <a href="#">Home</a>
            </li>
          </ul>
        </div>
        <!--/.nav-collapse -->
      </div>
    </nav>
    <div class="container">
      <div class="starter-template">
        <form id="ip-search">
          <label for="ipaddr" class="sr-only">IP address</label>
          <input type="text" id="ipaddr" class="form-control" placeholder="IP address here" required="" autofocus="">
          <button class="btn btn-lg btn-primary btn-block" type="submit">Search</button>
        </form>
        <div id="result"></div>
      </div>
    </div>
    <!-- /.container -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="http://underscorejs.org/underscore-min.js"></script>
    <script id="result-template" type="text/template">
      <br/><hr/>
      <table class="table">
        <tbody>
          <tr>
            <th>IP address</th>
            <td>
              <%= ip %>
            </td>
          </tr>
          <tr>
            <th>Country</th>
            <td>
              <%= country %>
            </td>
          </tr>
          <tr>
            <th>Org</th>
            <td>
              <%= org %>
            </td>
          </tr>
          <tr>
            <th>Addr</th>
            <td>
              &#x3012;<%= postal %><br/>
              <%= region %> <%= city %>
            </td>
          </tr>
        </tbody>
      </table>
    </script>
    <script type="text/javascript">
      function callback(data) {
        var template = $("#result-template").text();
        var compiled = _.template(template);
        $("#result").html(compiled(data));
      }
      $(function(){
        $('#ip-search').on('submit', function(){
          $.get("http://ipinfo.io/" + $('#ipaddr').val() + "/?callback=callback");
          return false;
        });
      });
    </script>
  </body>

</html>