I’m trying to use jQuery’s .AJAX function to send a request to IPINFODB’s API to get the geolocation of the user(s) visiting our site.
Problem is, from what I can gather jQuery’s .AJAX function doesn’t allow cross-domain requests and in-turn, returns nothing.
The following code alerts out [blank]
$.ajax({
type: "POST",
url: "http://api.ipinfodb.com/v3/ip-city/ip_query.php",
data: "key=***********&format=json&ip=<?php echo $_SERVER['REMOTE_ADDR']; ?>",
success: function(r) {
alert(r);
}
});
I’ve tried all variations of parameters for the .AJAX request such as GET, JSON, blah blah blah but still nothing. Does anyone know of another way of making a request to this API via AJAX? Preferably not using YQL.
This can be done with AJAX. You will need to use JSONP to get around the cross domain issues.
Here is the code:
You will need to replace
API_KEYwith your own API Key.