【vue】前端获取用户真实IP地址 (外网IP地址)

1. 打开index.html

<!-- 引入JS -->

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

 2. 获取IP 

let IP = returnCitySN["cip"];

localStorage.setItem('ip',IP);

 完整代码:

<!DOCTYPE html>
<html lang="zh-CN">

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>
            <%= htmlWebpackPlugin.options.title %>
        </title>

        ...省略其他代码

        <!-- 关键点1:引入JS -->
        <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>

    </head>

    <body>
        <noscript>
            <strong>Please enable JavaScript to continue.</strong>
        </noscript>
        <div id="app"></div>
        <!-- built files will be auto injected -->

            
        <script> 
             //这里是关键点2: 获取用户真实IP地址(外网ip) 并存储在本地
            // document.write(returnCitySN["cip"]+','+returnCitySN["cname"]) 
            localStorage.setItem('ip',returnCitySN["cip"])
        </script>
    </body>

</html>

猜你喜欢

转载自blog.csdn.net/q1ngqingsky/article/details/128081609