-2

Newby question here, I'm new to JAVAscript.

I need to do some simple stuff. I'm doing a jquery through $getJSON as:

$.getJSON(Flask.url_for("articles"), parameters).done(function(data, textStatus, jqXHR){
    var teste1=JSON.parse(jqXHR.responseText);
    console.log(".done");
    console.log(teste1);

});

The console.log is printing exactly what I need, but I'm not being able to get teste1 out of the $.getJSON. I could get a JSON object but the responseText is undefined and I can't parse it afterwards.

In resume I need the teste1 out of the $.getJSON.

I tried to return it but nothing really worked. I get the JSON object but I'm unable to get the information inside of it.

1 Answers1

0

Since the getJSON method is async, return is not a good option here. You can call a method from the done event by passing the response like this,

$.getJSON(Flask.url_for("articles"), parameters).done(function(data, textStatus, jqXHR){
    var teste1=JSON.parse(jqXHR.responseText);
    console.log(".done");
    parseResponse(teste1);

});

function parseResponse(response){
    console.log(response);
}
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
  • Thanks, the problem it's that I still got this in the function that calls the $.getJSON. I need to output the "response" to other function up in the code. Maybe I should assign to a global variable?!? – Conrado Calvet Oct 18 '17 at 08:39