$. getJSON - How to store responseJSON in a Variable?

Asked

Viewed 1,453 times

0

I am mounting a Javascript object with the following constructor:

var jsonResult = ""; // Inciei uma variável fora do escopo da Função

function criarDicionario(lang) {
    this.lang = lang;
    this.dicio = $.getJSON('pt-br.json').done(function(data) {
        alert(data.responseJSON); /* O Alert exibe corretamente o responseJSON */
        jsonResult = data.responseJSON;
    });

    console.log(this.dicio); // Devolve Objeto
    console.log(jsonResult); // Devolve Undefined
    /* Gostaria de armazenar this.dicio.responseJSON em uma variável */

    this.renderMessage = function(text) {
        document.write(this.dicio.responseJSON['text']);
    }
}

What I would like is that variable this.dicio.responseJSON could be stored in such a way that I could call it later in function renderMessege().

3 answers

1

$.getJSON is asynchronous, so the value returned by it will be undefined on the next line, and also the responseJSON will be inside dicio. I suggest you perform the next operations inside the callback done, something more or less like:

function criarDicionario(lang) {
    this.lang = lang;
    $.getJSON('pt-br.json').done(function(data) {
        this.dicio.responseJSON = data.responseJSON;
        console.log(this.dicio.responseJSON);
    });

    this.renderMessage = function(text) {
        document.write(this.dicio.responseJSON['text']);
    }
}
  • I could call the renderMessage from inside the .done?

  • In my opinion you could leave the renderMessage out of Function criarDicionario..what you think?

0

I believe this would work

function criarDicionario(lang) {
    this.lang = lang;
    var dicio;
    $.getJSON('pt-br.json').done(function(data){
        dicio = data.responseJSON;
        alert(data.responseJSON); /* O Alert exibe corretamente o responseJSON */
    });

    console.log(dicio); // Devolve Objeto (aqui ja vai ser o objeto de responseJSON)
    /* Gostaria de armazenar this.dicio.responseJSON em uma variável */

    this.renderMessage = function(text) {
        document.write(dicio['text']);
    }
}

Summarizing what happens is that the variable dicio was guarding the object that makes the requisition and not the answer of the requisition, why ajax is the same.

That’s why I’m setting the variable dicio in the done which is the function that is executed when the request is finished.

Another little problem is this basically it points to the function itself, ie will look for a variable called dicio within the function renderMessage() as Victor had stated dicio inside criarDicionario() it did not exist in the scope of renderMessage(). Note that I took the this var dicio.

0

I think it might help you, I’ve rebuilt your function and I think it’s pretty risky.

But anyway, here it is:

            var jsonResult;

            function criarDicionario(lang){
              this.lang = lang;
              this.dicio = $.getJSON("maicon.php").done(function(data) {
                    jsonResult = data;
                });
            }

             criarDicionario("pt-br");

In this example I am handing you the show of what I did... I hope to have helped!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.