js根据ip自动获取地址(省市区)

HTML:

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>标题</title>
<script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
<script src="http://at.alicdn.com/t/font_kfbh2nlnqrrudi.js"></script>
<script src="https://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

JS:

  mounted () {/* eslint-disable */
this.city = remote_ip_info.city
// console.log(remote_ip_info.province + '\n' + remote_ip_info.city)
/* eslint-enable */
},

如果你看懂了上面的代码,就不用往下看了

讲解:

我们远程引入全局的一个js文件(这里也可以局部引入)

https://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

  这个文件返回的数据如下:

var remote_ip_info = {"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"","type":"","desc":""};

  参数讲解:

country:国家
province:省份
city:城市
isp:所用网络(例如电信)

好啦,你已经获取到你想要的省市区了。

上一篇:C# 数字带逗号(千分位符、金钱千分位字符)


下一篇:greenplum不能下载问题解决方法(转)