views:

469

answers:

5

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

+2  A: 

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.

jsight
A: 

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.

micmcg
A: 

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.

ayman
+2  A: 

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.

npdoty
A: 

Hi ayman,

I'm trying to call the ipinfodb.com service using jquery as you suggested but keep getting "Permission Denied", my code is:

var url = "http://ipinfodb.com/ip_query_country.php?output=json&jsoncallback=?;";

$.getJSON(url, function(data){
if(data['status'] == 'ok'){ ... }

Any idea?

Thanks

That's not really an answer to the question, but there's a couple of typos in your URL which might be causing your problem. You want the URL to end with ``.
npdoty