Lord Vermillion
Lord Vermillion

Reputation: 5414

ajax GET json data

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

Answers (3)

Ravinder Gujiri
Ravinder Gujiri

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

Yogesh Mangaj
Yogesh Mangaj

Reputation: 3280

You are passing a callback parameter to the API, remove it (&callback=listPlaces) from the URL-

http://www.geonames.org/childrenJSON?geonameId=2661886&style=long&noCacheIE=1400498525471&get_param=value

Upvotes: 1

Nitish Kumar
Nitish Kumar

Reputation: 4870

Remove callback attribute from url

It should be

http://www.geonames.org/childrenJSON?geonameId=2661886&style=long&noCacheIE=1400498525471

DEMO

Upvotes: 1

Related Questions