跨域相关文章: https://mp.csdn.net/postedit/85329628
(1)126api (速度较慢)
http://ip.ws.126.net/ipquery?ip=IP
https://ip.ws.126.net/ipquery?ip=IP (也支持https协议)
返回结果如下:
var lo="广东省", lc="广州市"; var localAddress={city:"广州市", province:"广东省"}
请求方式:
var url = "https://ip.ws.126.net/ipquery?ip=IP"; // 查询当前用户ip所在地理位置// var url = "https://ip.ws.126.net/ipquery?ip=36.32.224.255"; // 查询某一ip所在地理位置$.getScript(url, function(){ var cityName = lc; // 城市名 }); });
(2)搜狐api
http://pv.sohu.com/cityjson?ie=utf-8 接口在4G网络或者其他情况返回的城市代码为CN,城市名为CHINA,不够精确,不建议使用
(3)太平洋:
http://whois.pconline.com.cn/
(4)百度:
http://lbsyun.baidu.com/index.php?title=webapi/ip-api
百度天气:
https://www.cnblogs.com/wangchengshen/p/3668946.html
https://blog.csdn.net/ixiaoyang/article/details/73556701
(5)淘宝
淘宝获取本机IP地址接口地址:http://www.taobao.com/help/getip.php
传递参数:无
返回类型:jsonp
callback:ipCallback
返回值:
ip:本机IP地址请求示例:
Request URL:http://www.taobao.com/help/getip.php返回示例:
ipCallback({ip:"115.159.152.210"})备注:本接口只有返回IP地址的功能
淘宝获取IP详细信息
接口地址:http://ip.taobao.com/service/getIpInfo.php
传递参数:
ip:要查询的IP地址参数传递方式:GET/POST
返回类型:json
返回值:
code:错误码(为零代表请求成功)country:国名country_id:国名(英文缩写)area:地域(如:华东)area_id:地域IDregion:行政区region_id:行政区IDcity:城市名city_id:城市IDisp:网络提供商isp_id:网络提供商IDip:请求的IP地址请求示例:
Request URL:http://ip.taobao.com/service/getIpInfo.php?ip=115.159.152.210返回示例:
{ "code":0, "data":{ "country":"中国", "country_id":"CN", "area":"华东", "area_id":"300000", "region":"上海市", "region_id":"310000", "city":"上海市", "city_id":"310100", "county":"", "county_id":"-1", "isp":"腾讯网络", "isp_id":"1000153", "ip":"115.159.152.210" }}备注:本接口来自淘宝IP地址库
查询天气: 以下三方api地址均可使用http或https// 方式一: 太平洋API:查询当前用户位置信息 (推荐) $.ajax({ type: 'get', url: "https://whois.pconline.com.cn/ipJson.jsp", dataType: 'jsonp', data: { }, success: function (ret) { var cityName = ret.city; // 百度API:通过城市名查询天气 $.ajax({ type: 'get', url: 'https://api.map.baidu.com/telematics/v3/weather', dataType: 'jsonp', data: { location: cityName, output: 'json', ak: constant.WEATHER.AK }, success: function (res) { if (res.status === 'success') { var data = res.results[0]; var weather_data = data.weather_data[0]; var temperature = data.weather_data[0].date.split(':')[1].split(')')[0]; $('#position').text(data.currentCity); $('#temperature').text(temperature); $('#weather_now').text(weather_data.weather); $('#weather_temperature').text(weather_data.temperature.replace('~', '/')); } else { console.log('天气接口报错' + res.error); } }, error: function (error) { console.log(error); } }); } });// 方式二:百度API:查询当前用户位置信息 (推荐) $.ajax({ type: 'get', url: "https://api.map.baidu.com/location/ip", dataType: 'jsonp', data: { ak: constant.WEATHER.AK }, success: function (ret) { var cityName = ret.content.address; // 百度API:通过城市名查询天气 $.ajax({ type: 'get', url: 'https://api.map.baidu.com/telematics/v3/weather', dataType: 'jsonp', data: { location: cityName, output: 'json', ak: constant.WEATHER.AK }, success: function (res) { if (res.status === 'success') { var data = res.results[0]; var weather_data = data.weather_data[0]; var temperature = data.weather_data[0].date.split(':')[1].split(')')[0]; $('#position').text(data.currentCity); $('#temperature').text(temperature); $('#weather_now').text(weather_data.weather); $('#weather_temperature').text(weather_data.temperature.replace('~', '/')); } else { console.log('天气接口报错' + res.error); } }, error: function (error) { console.log(error); } }); } }); // 方式三:126API:查询当前用户位置信息 (速度较慢) $.getScript("https://ip.ws.126.net/ipquery?ip=IP", function(){ var cityName = lc; // 百度API:通过城市名查询天气 $.ajax({ type: 'get', url: 'https://api.map.baidu.com/telematics/v3/weather', dataType: 'jsonp', data: { location: cityName, output: 'json', ak: constant.WEATHER.AK }, success: function (res) { if (res.status === 'success') { var data = res.results[0]; var weather_data = data.weather_data[0]; var temperature = data.weather_data[0].date.split(':')[1].split(')')[0]; $('#position').text(data.currentCity); $('#temperature').text(temperature); $('#weather_now').text(weather_data.weather); $('#weather_temperature').text(weather_data.temperature.replace('~', '/')); } else { console.log('天气接口报错' + res.error); } }, error: function (error) { console.log(error); } }); });
曾经有过的-百度高精度IP定位服务
https://www.jianshu.com/p/ae46bc471ac2