1

I want to make a global variable of the result of the $.getJSON request. If I do it as in my code below I get the a undefined message.

This is my .js code:

      var test = $.getJSON("dropdown_code/get_tables.php", success = function(data)
    {
        var options = "";
    for(var i = 0; i < data.length; i++)
        {
            options += "<option value='" + data[i] + "'>" + data[i] + "</option>";
        }

    $("#slctTable").append(options);
    $("#slctTable").change();
    return data;
}); 

console.log(test);

$("#slctTable").change(function()
{
$.getJSON("dropdown_code/get_fields.php?table=" + $(this).val(), success = function(data)
    {
        var options = "";
    for(var i = 0; i < data.length; i++)
        {
            options += "<option value='" + data[i] + "'>" + data[i] + "</option>";
        }
    $("#slctField").html("");
    $("#slctField").append(options);
    $("#slctField").change();
    return data;
});    
});
}); 
Jordi Z
  • 179
  • 13
  • Because you are trying to access the variable before it is getting assigned – Pawan Nogariya May 19 '16 at 10:59
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Scimonster May 19 '16 at 10:59

2 Answers2

1
    var tableList = $.getJSON("dropdown_code/get_tables.php", success = function(data)
                        {
                            var options = "";
                        for(var i = 0; i < data.length; i++)
                            {
                                options += "<option value='" + data[i] + "'>" + data[i] + "</option>";
                            }

                        $("#slctTable").append(options);
                        $("#slctTable").change();
                        return data;
                    }); 
     console.log(tablelist);
  • Is it possible to use the global var tablelist in another .js file now? – Jordi Z May 19 '16 at 11:17
  • quick alternative is to store var `tablelist` to loaclStorage `localStorage.setItem("tablelist",tablelist)` and retrieve it in the other .js file `var tablelist = localStorage.getItem("tablelist")` @JordiZ –  May 19 '16 at 11:24
  • Can't you just use tablelist as a global variable? – Jordi Z May 19 '16 at 11:36
  • yes you can but it has to do with the way you include your js files so this is helpful in that case http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files, both ways are correct anyway. –  May 19 '16 at 11:39
  • Ok thanks for the information. I've one last question: I update my .js file and i want to make it a variable once again. The problem is that it has a .change function. Do you know how to solve this too? – Jordi Z May 19 '16 at 11:57
  • can you edit your question posting more code in order to understand better? @JordiZ –  May 19 '16 at 12:21
  • I updated the code, this is how the whole file looks like – Jordi Z May 19 '16 at 12:49
0

When browser execute

   console.log(tableList); 

it's not yet assigned.

You can print it only after execution of this line

tableList=data;
Ygalbel
  • 5,214
  • 1
  • 24
  • 32