浅谈js获取客户端IP

JS前端获取客户端IP的方法基本都是通过三方接口:

常用的方法1:

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
document.write(returnCitySN["cip"]+','+returnCitySN["cname"])
</script>

常用的方法2:

var first = document.body.firstChild;
var oScript = document.createElement("script");
oScript.src =
"http://pv.sohu.com/cityjson?ie=utf-8&callback=returnCitySN&t=" + new Date().getTime();
document.body.insertBefore(oScript, first);
document.onclick = () => console.log(returnCitySN)

看输出就是IP地址了

posted @ 2019-06-26 14:17  lwming  阅读(2931)  评论(1编辑  收藏  举报