如何用js获取ip地址
获取 ip 地址的方法:直接获取webrtc api。使用 navigator.mediadevices.getusermedia()。通过服务器代理发送 ajax 或 fetch 请求。
如何用 JavaScript 获取 IP 地址
直接获取
- WebRTC API
async function getIP() { const configuration = { iceServers: [ { urls: ['stun:stun.l.google.com:19302'] } ] }; const peerConnection = new RTCPeerConnection(configuration); const iceCandidate = await new Promise((resolve) => { peerConnection.onicecandidate = (e) => { if (e.candidate && e.candidate.type === 'srflx') { resolve(e.candidate.address); } }; }); peerConnection.close(); return iceCandidate; }
发表评论