-1

I am somehow stuck. Probably this is an easy question, but I just can't get it working.

I am trying to access a response from an API (mapquest), but I can not find a way to dig into the reponse to extract the relevant information. Here is my console-log and my code, I actually want to access the responseJSON and some stuff in there (results).

var convertAddress = function(){
    var PLZ = $("#PLZ").val();
    var Ort = $("#Ort").val();
    var Landkreis = $("#Landkreis").val();
    var Umkreis = $("#Umkreis").val();
    
    
    document.getElementById("lokalisierung").style.zIndex = "-1"; 
    var url = 'http://open.mapquestapi.com/geocoding/v1/address?key=NEYE0LPf4hbAccEoG98DQbrRt5RB1700&location=Germany,' + PLZ ;
            
    var D = $.ajax({
        type: "GET",
        url: url,
        dataType: "json"
        });
//    console.log(PLZ);
//    console.log(Ort);
//    console.log(Landkreis);
//    console.log(Umkreis);
    console.log(D);
    console.log(JSON.stringify(D));
    console.log(Object.keys(D));
};

results

I found this stringify stuff on SO; but nothing worked as I expected, so probably there is a basic problem here.

Felix
  • 91
  • 1
  • 1
  • 10
  • Please post code in the question, not pictures of code – Jaromanda X Sep 25 '20 at 11:06
  • looks like you don't understand the asynchronous nature of $.ajax ... see https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – Jaromanda X Sep 25 '20 at 11:07
  • @JaromandaX Sorry for the picutre, I will update the question and check your link. I am quite new and have to learn while doing it, so I did not even hear of the asynchronous nature of ajax. Thanks for the link! – Felix Sep 25 '20 at 11:11

1 Answers1

1

You can access ur data with .done method. More information in official documentation jQuery Ajax

$.ajax(...)
  .done(function(data) {
    // ur data
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

Done method is called when async call is done.

Ashwel
  • 1,194
  • 9
  • 16
  • Thank you very much! this works now! I think I got the idea of asynchronous flows as well, which is obviously helpful. – Felix Sep 25 '20 at 11:19