1. 首先写一个js文件,areaLocation.js:
var provinceArray= new Array()
provinceArray=[
{"pname":"江苏",
"country":[
{ "cname":"南京市",
"town":["玄武区","白下区","秦淮区","建邺区","雨花台","江宁区", "六合区","溧水县","高淳县"]},
{ "cname":"南通市",
"town":["崇川区","港闸区","启东市","如皋市","通州市","海门市","海安县","如东县"] },
{ "cname":"苏州市",
"town":["金阊区","沧浪区","平江区","虎丘区","吴中区","张家港","昆山市","吴江市","太仓市"] }
]
},
{"pname":"上海",
"country":[
{ "cname":"上海市",
"town":["徐汇区","普陀区","杨浦区","虹口区","卢湾区","浦东区","黄浦区","长宁区","闸北区","静安区","闵行区","松江区"]
}
]
}
]
如果只是写浏览器端js的话,需要一个庞大的js问价的ip数据库// 数据库
var database = {
'123.123.123.123': {
province: '北京市',
city: '北京市',
operators: '联通',
},
'123.123.123.124': {
province: '北京市',
city: '北京市',
operators: '联通',
}
// ...
}
// 主函数
var inquireIP = function (ip) {
if(database[ip]) {
return database[ip]
}else{
console.log('找不到ip:' + ip)
return null
}
}
// 调用
var ipData = inquireIP('123.123.123.123')
这样子这个js文件会很大,也可以调用API的形式,为方便下面js代码使用了jquery
// 主函数
var inquireIP = function (ip, callback) {
$.ajax({
url: '你的API接口地址',
type: 'GET',
dataType: 'jsonp',
data: {ip: ip}
})
.done(function(data) {
callback(data)
})
}
// 调用
inquireIP('123.123.123.123', function (data) {
console.log(data)
})
这样子就可以在服务端处理,当然提前你得有服务器,并搭建一个查询ip的API接口
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)