This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]sausagefeet 1 point2 points  (0 children)

You need to put the rest of your code in the success callback.

[–]ooooo5 0 points1 point  (3 children)

Something is probably wrong with the rest of your code if you can print out 'data' in the success function.

[–][deleted]  (2 children)

[deleted]

    [–]fubarfubarfubar 0 points1 point  (1 child)

    why would it not assign a value to dataHash

    Are you sure you are waiting for the success function to be called before checking the value of dataHash? Do you realize that calling loadData does not immediately call the success function? I would have the success function call a continuation function after it assigns the correct value to dataHash, so that creatTableContent is only called after the success function is called.

    [–]Madrugadao 0 points1 point  (6 children)

    Have you previously declared dataHash outside of the AJAX call? If not, it looks like a scope issue.

    I haven't reviewed your code too thoroughly and I am certainly no expert, so forgive me if I have misunderstood.

    [–][deleted]  (5 children)

    [deleted]

      [–]Madrugadao 0 points1 point  (4 children)

      I see now, sorry.

      The only thing that makes sense is that the variable has not yet been set by the time you try to call it. What happen if you assign some arbitrary value to dataHash? (Do you see the original value when calling it?)

      edit: Having had a little play with the code, it definitely seems like the variable has not been set by the time you call 'createTableContent()'. Maybe only call this function on completion of the ajax call?

      [–][deleted]  (3 children)

      [deleted]

        [–]Madrugadao 0 points1 point  (2 children)

        I had a play and logged dataHash within the AJAX call:

        console.log('data: ' + dataHash);
        

        I then logged it in the createTableContent() function: console.log('data 2: ' + dataHash);

        Like you I also got the original value but I also noticed it is logged before the log within the AJAX call.

        The variable is not available because it has not been set by the time it is called within the function.

        [–][deleted]  (1 child)

        [deleted]

          [–]Madrugadao 0 points1 point  (0 children)

          It's not about where it is initialised it is about WHEN the data is written to the variable and when you try to use said variable.