Search code examples
javascriptgeolocation

Country-level geolocation in JavaScript using IP lookup?


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.net/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.


Solution

  • Just add the callback parameter.

    From freegeoip webpage:

    JSON callbacks are supported by adding the callback argument to the query string