简体   繁体   中英

Get certain data from json (google geocode api)

I try to get a certain data from my json request from the google geocode api.

My code looks like this:

$('#test').on('focusout', function() {
    var address = $('#ustreet').val() + " " + $('#ustreetnr').val() + ", " + $('#uplz').val(); 
    $('#uaddress').val(address);
    $.ajax({
        url: 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address,
        dataType: 'jsonp',
        success: function(json) {
            console.log(json.geometry.location.lat);
            console.log(json);
        }
    });
});

My results from request are fine and looking like this:

http://maps.google.com/maps/api/geocode/json?address=Celler%20Weg%2055,%2021079

But I got problems to get just the "lat & lng" values from geometry->location and store it into a var.

ReferenceError: json is not defined

I am for sure a JavaScript/jQuery beginner but for an other project with another API it worked great the same way I tried it here ...

I hope I can get some help here :)

Greetings from poland

Change dataType: 'jsonp' to dataType: 'json' .

JSONP stands for JSON with Padding.

dataType: jsonp is for cross-domain requests, that means request to different domains. dataType: json for the same domain-same origin request.

 $.ajax({ url: 'https://maps.googleapis.com/maps/api/geocode/json?address=Prunesstraat 4 opheusden', dataType: 'json', success: function(json) { console.log(json); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM