豌豆Ai站群搜索引擎系统 V.25.10.25 网址:www.wd.chat

🗣 Wd提问: js获取当前服务器ip

🤖 Ai回答:
获取服务器IP地址的方法

由于JavaScript在浏览器中运行,无法直接获取服务器IP地址,但可以通过以下几种方法间接获取:

实现思路

1、使用第三方API查询服务器IP
2、通过JavaScript发送请求并解析响应头
3、使用WebRTC获取本地IP(仅客户端IP)

下面我将实现一个用户友好的界面,展示这些方法的结果。

html
<!DOCTYPE html
<html lang=zh-CN
<head
<meta charset=UTF-8
<meta name=viewport content=width=device-width, initial-scale=1.0
<title获取服务器IP地址</title
<style
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
background: linear-gradient(135deg, 6a11cb 0%, 2575fc 100%);
color: 333;
min-height: 100vh;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

.container {
background: rgba(255, 255, 255, 0.9);
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
width: 90%;
max-width: 800px;
padding: 30px;
margin: 20px 0;
}

h1 {
text-align: center;
margin-bottom: 20px;
color: 2575fc;
font-size: 2.2rem;
}

.description {
text-align: center;
margin-bottom: 30px;
color: 555;
line-height: 1.6;
}

.method-container {
margin-bottom: 25px;
padding: 20px;
border-radius: 10px;
background: f8f9fa;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.method-title {
display: flex;
align-items: center;
margin-bottom: 15px;
font-weight: 600;
color: 2575fc;
}

.method-icon {
margin-right: 10px;
font-size: 1.2rem;
}

.method-description {
margin-bottom: 15px;
color: 666;
}

.result-box {
background: e9ecef;
padding: 12px 15px;
border-radius: 8px;
margin-bottom: 15px;
word-break: break-all;
font-family: monospace;
min-height: 50px;
display: flex;
align-items: center;
}

.btn {
background: 2575fc;
color: white;
border: none;
padding: 12px 20px;
border-radius: 8px;
cursor: pointer;
font-size: 1rem;
font-weight: 600;
transition: all 0.3s ease;
display: inline-flex;
align-items: center;
justify-content: center;
}

.btn:hover {
background: 1a68e3;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(37, 117, 252, 0.4);
}

.btn:active {
transform: translateY(0);
}

.btn-icon {
margin-right: 8px;
}

.loading {
display: none;
color: 2575fc;
font-style: italic;
}

.error {
color: dc3545;
margin-top: 10px;
}

.note {
margin-top: 30px;
padding: 15px;
background: fff3cd;
border-radius: 8px;
border-left: 4px solid ffc107;
}

@media (max-width: 600px) {
.container {
padding: 20px;
}

h1 {
font-size: 1.8rem;
}

.btn {
width: 100%;
}
}
</style
</head
<body
<div class=container
<h1获取服务器IP地址</h1
<div class=description
<p由于JavaScript在浏览器中运行,无法直接获取服务器IP地址,但可以通过以下方法间接获取。</p
</div

<div class=method-container
<div class=method-title
<span class=method-icon🌐</span
<h2方法一:使用第三方API</h2
</div
<div class=method-description
<p通过调用第三方API服务获取当前服务器的公共IP地址。</p
</div
<div class=result-box id=api-result
<span点击下方按钮获取结果</span
</div
<button class=btn id=api-btn
<span class=btn-icon🔍</span
获取服务器IP
</button
<div class=loading id=api-loading正在获取IP地址...</div
<div class=error id=api-error</div
</div

<div class=method-container
<div class=method-title
<span class=method-icon📡</span
<h2方法二:解析请求头</h2
</div
<div class=method-description
<p通过向服务器发送请求并解析响应头来获取服务器信息。</p
</div
<div class=result-box id=header-result
<span点击下方按钮获取结果</span
</div
<button class=btn id=header-btn
<span class=btn-icon🔍</span
解析请求头
</button
<div class=loading id=header-loading正在解析请求头...</div
<div class=error id=header-error</div
</div

<div class=method-container
<div class=method-title
<span class=method-icon💻</span
<h2方法三:获取本地IP</h2
</div
<div class=method-description
<p使用WebRTC技术获取客户端的本地IP地址(非服务器IP)。</p
</div
<div class=result-box id=local-result
<span点击下方按钮获取结果</span
</div
<button class=btn id=local-btn
<span class=btn-icon🔍</span
获取本地IP
</button
<div class=loading id=local-loading正在获取本地IP...</div
<div class=error id=local-error</div
</div

<div class=note
<p<strong注意:</strong由于浏览器安全限制,JavaScript无法直接获取服务器IP地址。上述方法提供的是间接获取方式,结果可能因浏览器、网络环境和服务器配置而异。</p
</div
</div

<script
document.addEventListener('DOMContentLoaded', function() {
// 方法一:使用第三方API
document.getElementById('api-btn').addEventListener('click', function() {
const resultElement = document.getElementById('api-result');
const loadingElement = document.getElementById('api-loading');
const errorElement = document.getElementById('api-error');

// 重置状态
errorElement.textContent = '';
loadingElement.style.display = 'block';
resultElement.innerHTML = '<span正在获取...</span';

// 使用多个API以提高成功率
const apis = [
'https://api.ipify.org?format=json',
'https://api.myip.com',
'https://ipapi.co/json/'
];

let attempts = 0;

function tryApi(index) {
if (index = apis.length) {
loadingElement.style.display = 'none';
errorElement.textContent = '所有API请求都失败了,请检查网络连接或稍后重试。';
return;
}

fetch(apis[index])
.then(response = {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data = {
loadingElement.style.display = 'none';
let ip = '';

// 根据不同的API响应结构提取IP
if (data.ip) {
ip = data.ip;
} else if (data.ip_addr) {
ip = data.ip_addr;
} else if (data.query) {
ip = data.query;
}

if (ip) {
resultElement.innerHTML = `<strong服务器IP地址:</strong

Ai作答

📣 商家广告


广告招商

广告招商

星座

看相

vps


0

IP地址: 104.178.70.71

搜索次数: 385

提问时间: 2025-11-28 23:24:43

🛒 域名购买

❓️ 热门提问

🌐 域名评估

最新挖掘

🖌 热门作画

🤝 关于我们

🗨 加入群聊

🔗 友情链接

🧰 站长工具
📢

温馨提示

本站所有 问答 均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。

👉

技术支持

本站由 🟢 豌豆Ai 提供技术支持,使用的最新版: 豌豆Ai站群搜索引擎系统 V.25.10.25 搭建本站。

上一篇 83935 83936 83937 下一篇