How To Access Geo Ip Look Up With Javascript
I'd like ot know how I can consume the output of this service with JavaScript. It's not valid JSON http://geoiplookup.wikimedia.org/ EDIT Here is how I was hoping to retrieve th
Solution 1:
I see it's response contains a string which can be easily processed with simple JS. Try this.
var geoStr = 'Geo = {"city":"Woodbury","country":"US","lat":"39.824001","lon":"-75.131798","IP":"98.110.51.114","netmask":"24"};';
var geoJSON = geoStr.split('=')[1];
geoJSON = $.parseJSON(geoJSON.substring(0, geoJSON.length -1));
console.log(geoJSON);
Working Demo
Solution 2:
jQuery and AJAX will do the trick. Like ShankarSangoli said, this will work cross-domain. Here is an working example:
var query = "select * from html where url='http://geoiplookup.wikimedia.org'"var encodedQuery = encodeURIComponent(query.toLowerCase()),
url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&diagnostics=true&callback=?';
$.ajax({
dataType: "json",
url: url,
async: false,
success: function (data) {
var w = data.query.results.body.p;
var geoJSON = w.split('=')[1];
geoJSON = $.parseJSON(geoJSON);
var latitude = geoJSON.lat;
var longitude = geoJSON.lon;
console.log('Geo IP: ('+ latitude + ' - ' + longitude +')');
}
});
You can also try these links if you are working on a HTML5 Geolocation Fallback method: http://www.geoplugin.net/json.gphttp://freegeoip.net/json/
Post a Comment for "How To Access Geo Ip Look Up With Javascript"