// 使用第三方 API 获取用户 IP 地址
async function getUserIP() {
try {
// 调用第三方 API 获取用户的公共 IP 地址
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
// 返回获取到的 IP 地址
return data.ip;
} catch (error) {
console.error('获取 IP 失败:', error);
return null;
}
}
// 调用函数并输出 IP 地址
getUserIP().then(ip => {
if (ip) {
console.log('用户的 IP 地址是:', ip);
} else {
console.log('无法获取 IP 地址');
}
});
fetch
请求:我们使用 fetch
方法向 https://api.ipify.org?format=json
发送请求,该 API 会返回用户的公共 IP 地址。async/await
来简化代码逻辑。null
。上一篇:js foreach map
下一篇:js worker 多线程
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站