Geolocation using hostip with javascript

This is probably a very stupid question. I am trying to do geolocation to find a user's address based on their ip address using the api provided by http://www.hostip.info/use.html . I am using this in conjunction with jquery with the following code:

$.get("http://api.hostip.info/get_html.php", function(data){
    alert("Data Loaded: " + data);
});

      

Unfortunately this doesn't work. The alert never fires, so my guess is that the call never returns. Has anyone done this before? Thanks to

+2


source to share


4 answers


I don't think the call to them is successful. Cross domain restrictions are likely to prevent you from reading the result data. If hostip offered a JSONP API , that would be helpful, but I haven't seen any mention of one on their site.



+4


source


If you are using the Google AJAX API, then it is really easy to get the location using the Client Location functionality - it doesn't require any cross-domain calls.

if (google.loader.ClientLocation) {
    var lat = google.loader.ClientLocation.latitude;
    var lon = google.loader.ClientLocation.longitude;
    ...

      



Otherwise, as others have pointed out, you will need a service that provides JSONP, or you will need to write a proxy on your server to get the data for you.

+2


source


You cannot make cross-domain calls to XML data. Other sites offer a JSON interface:

http://ipinfodb.com/ip_query.php?ip=999.999.999.999&output=json

      

which you can use for cross-domain calling using YUI GET Utility or via JQuery.

0


source


You cannot make calls to other people's domains from javascript. This is definitely a problem. You either need to set up a proxy script on your server that fetches the remote pages for you, or find a service than JSONP implements as above.

-1


source







All Articles