Drum Drum - 3 months ago 6
Javascript Question

How To Use JSON Returned From a URL in Javascript?

I have a URL which returns JSON data. I would then like to use some parts of this data in Javascript. At the moment I'm getting an 'Uncaught Syntax Error' in the console when I run it.

My code looks like this:

var myAPIcall = "https://maps.googleapis.com/maps/api/geocode/json?address=SW1A1AA&callback=?";


$.get(myAPIcall, function( data ) {
console.log("Place ID = " + data.results.place_id);
}, "json" );


It's the JSON itself which is causing the error.

How can I get the JSON (specifically the place_id, but I'd be happy with any of it) to display with the
console.log
? If I understand that much I should then be able to move on and use the place_id in my code.

Thanks

Answer

Check our query. Removing callback=? will result in a valid JSON :-)

var myAPIcall = "https://maps.googleapis.com/maps/api/geocode/json?address=SW1A1AA";


    $.getJSON(myAPIcall, function( data ) {
        console.log(data.results[0].place_id);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments