Drum Drum - 1 year ago 100
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
? If I understand that much I should then be able to move on and use the place_id in my code.


Answer Source

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 ) {
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download