Reputation: 5414
I'm trying to access this JSON data: http://www.geonames.org/childrenJSON?geonameId=2661886&callback=listPlaces&style=long&noCacheIE=1400498525471&get_param=value
The reponse is formated like this
listPlaces(
{
totalResultsCount: 21,
geonames: [
{
countryId: "2661886",
adminCode1: "02",
countryName: "Sweden",
fclName: "country, state, region,...",
countryCode: "SE",
lng: "15.33333",
fcodeName: "first-order administrative division",
toponymName: "Blekinge",
fcl: "A",
numberOfChildren: 5,
name: "Blekinge",
fcode: "ADM1",
geonameId: 2721357,
lat: "56.33333",
adminName1: "Blekinge",
population: 152315
}
I use this ajax method to access the data, but my console.log(data) shows nothing. I don't know how to handle the fact that the response starts with listPlaces( and then json-data inside that.
$.ajax({
type: 'GET',
url: 'http://www.geonames.org/childrenJSON?geonameId=2661886&callback=listPlaces&style=long&noCacheIE=1400498525471',
data: { get_param: 'value' },
dataType: 'json',
success: function (data) {
console.log(data);
$.each(data.geonames, function (index, element) {
//Todo
});
}
});
What am i dong wrong?
Upvotes: 1
Views: 212
Reputation: 1514
this api is used like this
<script type="text/javascript">
function listPlaces(data){
alert(data.totalResultsCount);
...
}
</script>
<script src="http://www.geonames.org/childrenJSON?geonameId=2661886&callback=listPlaces&style=long&noCacheIE=1400498525471"></script>
here callback listPlaces
should be a function which receives JSON data as parameter
and must be declared before calling script
and for ajax remove the callback variable from url
Upvotes: 2
Reputation: 3280
You are passing a callback
parameter to the API, remove it (&callback=listPlaces
) from the URL-
Upvotes: 1
Reputation: 4870
Remove callback attribute from url
It should be
http://www.geonames.org/childrenJSON?geonameId=2661886&style=long&noCacheIE=1400498525471
Upvotes: 1