<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>检测ip和地区</title> </head> <script src="js/jquery.min.js"></script> <style type="text/css"> .box{text-align: center;} </style> <body> <div class="box"> <h1>AJAX检测ip和地区</h1> <p id="city"></p> <p id="ip"></p> </div> </body> </html> <script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ //获取城市ajax $.ajax({ url: 'http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6', type: 'POST', dataType: 'jsonp', success:function(data) { console.log(JSON.stringify(data.content.address_detail.province + "," + data.content.address_detail.city)); $('#city').html(JSON.stringify(data.content.address_detail.province + "," + data.content.address_detail.city)) } }); //获取ip ajax $.ajax({ url: 'http://freegeoip.net/json/', success: function(data){ console.log(JSON.stringify(data.ip)); $('#ip').html(JSON.stringify(data.ip)) }, type: 'GET', dataType: 'JSON' }); }) </script> 提示:你可以先修改部分代码再运行。 转载请注明:有爱前端 » AJAX检测ip和地区 喜欢 (0)or分享 (0)