Last active
May 21, 2023 00:51
-
-
Save R41D3NN/b04369e9ed38ad1519fb to your computer and use it in GitHub Desktop.
Javascript for retrieving IP Address info using ipinfo.io API via AJAX.
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
var GetIpInfo = function(ipAddr) { | |
var info = null; | |
var infoUrl = "http://ipinfo.io/" + ipAddr; | |
$.ajax({ | |
url: infoUrl, | |
type: 'GET', | |
dataType: 'json', | |
async: false, | |
success: function(data) { | |
info = data; | |
} | |
}); | |
return info; | |
}; | |
// Convenience functions | |
var GetCoord = function(ipAddr) { | |
return GetIpInfo(ipAddr).loc; | |
}; | |
var GetCity = function(ipAddr) { | |
return GetIpInfo(ipAddr).city; | |
}; | |
var GetCountry = function(ipAddr) { | |
return GetIpInfo(ipAddr).country; | |
}; | |
var GetHostname = function(ipAddr) { | |
return GetIpInfo(ipAddr).hostname; | |
}; | |
var GetOrg = function(ipAddr) { | |
return GetIpInfo(ipAddr).org; | |
}; | |
var GetPhone = function(ipAddr) { | |
return GetIpInfo(ipAddr).phone; | |
}; | |
var GetPostal = function(ipAddr) { | |
return GetIpInfo(ipAddr).postal; | |
}; | |
var GetRegion = function(ipAddr) { | |
return GetIpInfo(ipAddr).region; | |
}; |
nice, but how to do it if i don't have the ipAddr?
i can call http://ipinfo.io/json and get a json, but i dont have any idea how to use it in javascript
Do you resolve this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice, but how to do it if i don't have the ipAddr?
i can call http://ipinfo.io/json and get a json, but i dont have any idea how to use it in javascript