JavaScript不能直接获取用户的IP地址,只能通过调用API的方法来获得。
可以通过以下几个地址获得:
1616.net
http://chaxun.1616.net/s.php?type=ip&output=json
function getadress() {
$.ajax({
url: "http://chaxun.1616.net/s.php?type=ip&output=json",
dataType: "jsonp",
success: function(data) {
console.log(data.Ip + "-" + data.Isp);
},
error: function(data) {
}
});
}
getadress();
百度
http://api.map.baidu.com/location/ip?ak=要到开放平台获取ak&coor=bd09ll
function getadress() {
$.ajax({
// 百度需要获取ak
// url: "http://api.map.baidu.com/location/ip?ak=先获取Ak&coor=bd09ll",
dataType: "jsonp",
success: function(data) {
console.log(data);
},
error: function(data) {
}
});
}
getadress();
搜狐
http://pv.sohu.com/cityjson
http://pv.sohu.com/cityjson?ie=utf-8 可设置编码
<script charset="utf-8" src="http://pv.sohu.com/cityjson" type="text/javascript"></script>
<script>
console.log(returnCitySN["cip"], returnCitySN["cname"]);
</script>
coding123
http://www.coding123.net/getip.ashx?js=1
<script type="text/javascript" src="http://www.coding123.net/getip.ashx?js=1"></script>
<script>
console.log(ip);
</script>
新浪
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=[ip地址]
<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>
<script type="text/javascript">
console.log(remote_ip_info["country"] + ',' + remote_ip_info["province"] + "省" + ',' + remote_ip_info["city"] + "市"); //中国,江苏省,南京市
</script>
评论区