API:获取当前用户的公网IP

在vue项目根目录下“ public ” 文件夹中的index.html,也就是根节点所在的文件引入JS,vue项目中静态文件需要在这里引入,用 script 标签规避跨域

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>

由于返回的是JSON,直接在需要使用IP的页面中打印即可:

console.log(returnCitySN["cip"]+','+returnCitySN["cname"])

得出想要的结果:

返回对象结构:

- END -

 

posted @ 2021-02-01 11:06  97z4moon  阅读(595)  评论(0)    收藏  举报
Title