Could someone give an example of how to do country-level geolocation in JavaScript, using IP lookup, rather than geolocation?
I keep falling foul of the JSONP cross-site restrictions, and I haven't yet found a good answer on StackOverflow. I was hoping for something like this:
$.getJSON("/", function(rs){
if (rs.country_name) {
alert(rs.ip, rs.country_name);
}
})
.error(function(e){
});
But that isn't allowed because it isn't JSONP. Does anyone know of a (preferably free) JSONP service? It doesn't have to be absolutely bulletproof.
Could someone give an example of how to do country-level geolocation in JavaScript, using IP lookup, rather than geolocation?
I keep falling foul of the JSONP cross-site restrictions, and I haven't yet found a good answer on StackOverflow. I was hoping for something like this:
$.getJSON("http://freegeoip/json/", function(rs){
if (rs.country_name) {
alert(rs.ip, rs.country_name);
}
})
.error(function(e){
});
But that isn't allowed because it isn't JSONP. Does anyone know of a (preferably free) JSONP service? It doesn't have to be absolutely bulletproof.
Share Improve this question asked Feb 21, 2012 at 21:54 RichardRichard 33k30 gold badges111 silver badges146 bronze badges 1- Before anyone suggests it: I don't want to use HTML5 (or not HTML5, but you know what I mean) geolocation. – Richard Commented Feb 21, 2012 at 21:57
4 Answers
Reset to default 3Just add the callback parameter.
From freegeoip webpage:
JSON callbacks are supported by adding the callback argument to the query string
You can make your own, update it periodically. Here is a free source: http://software77/geo-ip/
Alternatively, you can use this: http://www.maxmind./app/javascript_city, which is free if you credit them and provide a link back to their site.
http://geoiplookup.wikimedia/ would be something like a jsonp service (it's not really jsonp), but I'm not sure about whether to call it "free". They won't like heavy usage from other sites...
Of course it might even have the advantage to be cached already as Wikipedia is a often-used site, but I nevertheless would advise to install an own lookup service.
You can use this free service, it's fast and also works on https.
$.get("https://api.teletext.io/api/v1/geo-ip", function(response) {
console.log(response);
});