有勇气的牛排博客

jquery 获取 IP地址与地理位置

有勇气的牛排 361 前端 2023-05-18 22:22:16

1. 接口

搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
获取城市位置:http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6

2 获取地理位置 具体到市

$(function () { $.ajax({ url: 'http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6', type: 'POST', dataType: 'jsonp', success: function (res) { console.log(res) console.log(res.address) console.log(res.content.address) console.log(res.content.address_detail.adcode) console.log(res.content.address_detail.city) console.log(res.content.address_detail.city_code) console.log(res.content.address_detail.district) console.log(res.content.address_detail.province) console.log(res.content.address_detail.street) console.log(res.content.address_detail.street_number) } }); });
<script src="jquery.min.js"></script> <div> <input id="province" type="text"> </div> <button id="submit">提交</button> <script src="position.js"></script>

3 获取IP地址

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
$("span").html(returnCitySN["cip"]);
<span></span>

image.png


留言

专栏
文章
加入群聊