giroy
giroy

Reputation: 2243

Geolocation using hostip with javascript

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

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

Sadly, this doesn't seem to work. The alert is never triggered so i assume the call never returns. Has anyone done this before? Thanks

Upvotes: 2

Views: 2426

Answers (4)

npdoty
npdoty

Reputation: 4757

If you use Google's AJAX API, then it's really easy to get location using their Client Location functionality -- this 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'll need a service that provides JSONP or you'll need to write a proxy on your own server to get the data for you.

Upvotes: 2

ayman
ayman

Reputation: 1341

You can't make cross-domain calls for XML data. Other sites offer a JSON interface:

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

which you can cross-domain call using the YUI GET Utility or via JQuery.

Upvotes: 0

micmcg
micmcg

Reputation: 2390

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

Upvotes: -1

jsight
jsight

Reputation: 28419

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

Upvotes: 4

Related Questions