Last active
August 17, 2020 21:41
-
-
Save ccurtin/3ffe01e8e312805f38ec0c4146495603 to your computer and use it in GitHub Desktop.
Get User's Public IP Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FREE: no request linit for ipify.org | |
var request = new XMLHttpRequest() | |
var ipAddress = '' | |
request.open('GET', "https://api.ipify.org?format=jsonp=", true) | |
var ipAddress = request.onload = function() { | |
if (request.status >= 200 && request.status < 400) { | |
// Success! | |
return ipAddress = request.responseText | |
} else { | |
// We reached our target server, but it returned an error | |
} | |
} | |
request.onerror = function() { | |
// There was a connection error of some sort | |
} | |
request.send() | |
// console.log(ipAddress) | |
// FREE for first 1000 requests per day | |
$.getJSON('http://ipinfo.io', function(data){ | |
console.log(data) | |
}) | |
/* | |
RETURN DATA: | |
{ | |
city:"Dumont" | |
country:"US" | |
hostname:"static-96-242-234-3.nwrknj.fios.verizon.net" | |
ip:"96.242.234.3" | |
loc:"40.9467,-73.9930" | |
org:"AS701 MCI Communications Services, Inc. d/b/a Verizon Business" | |
postal:"07628" | |
region:"New Jersey" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment