一款jQuery调用ajax函数获取当前IP地址和当前省市区位置效果代码。
使用方法
在页面底部引入需要用到的js代码
获取城市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' }); })