0

I am trying load some JSON data from a PHP backend, parse them and populate a div based on the JSON data I receive. I want to do this as soon as the window has been done loading. Right now I have it setup like this:

$(function(){
    $.getJSON("todo_action.php?getall=true", function(data){
        console.log(data);
    });
});

However, this approach is not working. But after the window has been done loading and I go to Chrome's developer console and run the code above, it works. Correct me if I am wrong, but I think this is not working because by default all AJAX requests are asynchronous and it has to do with the asynchronous property of AJAX, right?

I tried other variants of jQuery's built in AJAX functions like get, load, ajax but no avail. I have also tried making the request synchronous using $.ajaxSetup({async: false}); and it still does not work.

4

2 に答える 2

3

締めくくりを見逃しているだけではありません);か?

$(function(){
    $.getJSON("todo_action.php?getall=true", function(data){
        console.log(data);
    });
}); // <- here

それはうまくいくはずです、私は JsFiddle hereの例を持っています。

于 2013-06-22T21:39:52.757 に答える
0

ページが完全にロードされていることを確認してください。

$(window).load(function() {
 // executes when complete page is fully loaded, including all frames, objects and images
 $.getJSON("todo_action.php?getall=true", function(data){
        console.log(data);
    });
});
于 2013-06-22T21:39:37.307 に答える