Country-level geolocation in JavaScript using IP lookup? - Stack Overflow

Could someone give an example of how to do country-level geolocation in JavaScript, using IP lookup, ra

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
Add a ment  | 

4 Answers 4

Reset to default 3

Just 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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744788448a4593768.html

相关推荐

  • Country-level geolocation in JavaScript using IP lookup? - Stack Overflow

    Could someone give an example of how to do country-level geolocation in JavaScript, using IP lookup, ra

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信