演示
- 利用太平洋网络ip接口返回访客城市
- 不建议使用这种,网站启用https时pc端会拦截从不安全源加载链接
- 页面输出,在合适的位置加入
html 代码:<span class="area_city"></span>
js 代码:function IPCallBack(data){
document.querySelector(".area_city").innerHTML = data.city;
}
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function onScriptLoaded(id){
// 删除添加的script标签
document.body.removeChild(document.getElementById(id));
}
function sendReq(url){
let script = document.createElement("script");
script.src = url;
script.type = "text/javascript";
let ranId = guid();
script.setAttribute("id", ranId);
if(script.readyState){
// ie
script.onreadystatechange = function(){
if(this.readyState === "complete" || this.readyState === "loaded"){
onScriptLoaded(ranId);
}
}
}else{
// 其他
script.onload = function(){
onScriptLoaded(ranId);
}
}
document.body.appendChild(script);
}
sendReq("http://whois.pconline.com.cn/ipJson.jsp");
- 利用私人api接口输出访客城市
- 将上面的js代码换为以下即可
js 代码:function getCity(){
return new Promise((resolve, reject) => {
fetch("https://api.gmit.vip/Api/IP").then((res) => {
return res.json();
}).then((data) => {
resolve(data);
}).catch((e) => {
reject(e);
});
});
}
async function setCity(){
let city = await getCity();
try{
document.querySelector(".area_city").innerHTML = city.data.location.country.split(" ")[2];
}catch (e) {
}
}
setCity();
html 代码:<iframe width="300" height="30" frameborder="0" scrolling="no" src="http://whois.pconline.com.cn/ip.jsp"></iframe>
ip归属地查询API
json 代码:{
"status": "0",
"t": "",
"set_cache_time": "",
"data": [
{
"location": "广东省广州市 移动",
"titlecont": "IP地址查询",
"origip": "117.136.12.79",
"origipquery": "117.136.12.79",
"showlamp": "1",
"showLikeShare": 1,
"shareImage": 1,
"ExtendedLocation": "",
"OriginQuery": "117.136.12.79",
"tplt": "ip",
"resourceid": "6006",
"fetchkey": "117.136.12.79",
"appinfo": "",
"role_id": 0,
"disp_type": 0
}
]
}
json 代码:{
"ip": "117.136.12.79",
"pro": "广东省",
"proCode": "440000",
"city": "佛山市",
"cityCode": "440600",
"region": "",
"regionCode": "0",
"addr": "广东省佛山市 移动",
"regionNames": "",
"err": ""
}
json 代码:{"timestamp":1662911888472,"client_ips":["114.45.4.209"],"client_ip":"114.45.4.209"}
json 代码:{"success":true,
"Ip":"122.139.228.123",
"Country":"中国",
"Province":"吉林省",
"County":"二道区",
"City":"长春市",
"adcode":220105,
"Latitude":43.86497,
"Longitude":125.37427}
想问博主现在您这个IP属地是怎么实现的?
ip离线库获取的
暂无点赞
暂无点赞