获取单个公网ip

作者:Administrator 发布时间: 2024-09-10 阅读量:8 评论数:0

获取单个公网ip

根据上篇文章,我们获取到了很全的数据

如果我们只需要直观的看到ip地址怎么处理?

于是使用ChatGPT单独写了一个页面如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Awesome</title>
</head>
<body>
    <p id="ip">Fetching IP address...</p>

    <script>
        // 发送HTTP GET请求
        fetch('https://ip.dev6.site')
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error(`Failed to retrieve data. HTTP Status code: ${response.status}`);
                }
            })
            .then(data => {
                // 提取ip的值
                const ipValue = data.ip;
                if (ipValue) {
                    document.getElementById('ip').textContent = `${ipValue}`;
                } else {
                    document.getElementById('ip').textContent = "IP value not found in the response.";
                }
            })
            .catch(error => {
                document.getElementById('ip').textContent = `Failed to retrieve data. Error: ${error.message}`;
            });
    </script>
</body>
</html>

显示效果

image

评论