js obtains the ip address according to the current request

function getIP() {
	return new Promise((resolve, reject) => {
		const xhr = new XMLHttpRequest();
		xhr.open('GET', 'https://api.ipify.org?format=json');
		xhr.onload = () => {
			if (xhr.status === 200) {
				const response = JSON.parse(xhr.responseText);
				resolve(response.ip);
			} else {
				reject('Failed to get IP address');
			}
		};
	xhr.send();
	});
}


getIP().then(ip => console.log(ip)).catch(error => console.error(error));

Guess you like

Origin blog.csdn.net/m0_52191385/article/details/130973839