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

0

I am assembling 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 the variable this.dicio.responseJSON could be stored in such a way that I could call it later in function renderMessege() .

    
asked by anonymous 13.02.2017 / 17:56

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 in the pipeline. I suggest you carry out the next operations inside callback done, something 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']);
    }
}
    
13.02.2017 / 18:09
0

I believe this will 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']);
    }
}

In short, what happens is that the variable dicio was saving the object that makes the request and not the response of the request, because ajax is asynchronous.

So I'm setting the variable dicio to done which is the function that is executed when the request is finished.

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

    
13.02.2017 / 18:11
0

I think this can help you, I've rebuilt your job and I find it quite 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 giving you the show of what I did ... I hope I have helped !!

    
04.02.2018 / 03:47