Skip to content
Advertisement

Javascript access repsonse of API

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement