0

Having some trouble getting this to work, specifically with $.getJSON(). I want to wrap the getJSON function from jQuery in a Javascript function like so:

function reload_data() {
    $.getJSON("data/source", function(data) {
        $.d = data;
    });
}

But when I call reload_data() it doesn't execute the jQuery function inside. Any ideas?

flag
How do you know it isn't executing the function? Did you put a breakpoint on the line inside to check? – Nosredna Jul 1 '09 at 14:15
It's executing just fine. I guess it must be something about the data in the $.d variable not getting refreshed in a way that the rest of the script can use it. – mcmaloney Jul 1 '09 at 14:37
You're not showing us enough of the picture to give an accurate answer. – Josh Stodola Jul 1 '09 at 19:31
3  
Why are you hanging your "d" variable onto the jQuery namespace, by the way? – Nosredna Jul 1 '09 at 20:59

3 Answers

3

You're not telling us enough. I will just take a guess!

If you are calling this function, and then immediately checking $.d for the results, that is not going to work because you don't allow time for the asynchronous AJAX request to complete...

reload_data();
alert($.d); // What?!  It's not displaying the updated results?!

You'll have to utilize a callback structure, like jQuery uses, in order to make it work...

reload_data(function() {
  alert($.d);
});

function reload_data(func) {
  $.getJSON("data/source", function(data) {
    $.d = data;
    //Execute the callback, now that this functions job is done
    if(func)
      func();
  });
}
link|flag
Yeah, that was my guess too. That's why I asked if he was sure the function wasn't getting called. After all, Ajax is async. – Nosredna Jul 1 '09 at 19:46
That brings up an interesting point. I think he could change his request to be synchronous and that might fix the issue, although I don't think that's the best solution so I will leave my answer as is. – Josh Stodola Jul 1 '09 at 20:29
Either way, he's going to have to wait for the ajax to be performed before he can do anything with the variable. I think (but I'm not sure) that the browser's UI will be more responsive if the call is left async. – Nosredna Jul 1 '09 at 20:58
Absolutely. The async requests are smooth, sync ones cause the browser to hang. – Josh Stodola Jul 1 '09 at 21:24
0

Put an Alert in side the function to know its getting called.

and a try catch around the jQuery call to see if there is an error

function reload_data() {
    alert('reload_data  start');
    try{
        $.getJSON("data/source", function(data) {
            $.d = data;
        });
     }
     catch (ex){
         alert ('error in jQuery call:' + ex)
     }
}
link|flag
0

Thanks for the help everyone, but the solution was actually really simple. It was just a matter of synchronization. The page was reloading before the JSON data got reloaded, so that's why I wasn't seeing an error.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.