1

i'm new to JSON. I have a php with a multidimensional array which gets finally encoded in JSON format:

<?php
header('Content-Type: application/json');
$lista = array (
    'Conoscenti'=>array (
        0=>array(
            "Nome"=>"John",
            "Cognome"=>"Doe",
            "Nato"=>array(
                "Giorno"=>16,
                "Mese"=>"Febbraio",
                "Anno"=>1972
            )
        ),
        1=>array(
            "Nome"=>"Elvis",
            "Cognome"=>"Presley",
            "Nato"=>array(
                "Giorno"=>12,
                "Mese"=>"Luglio",
                "Anno"=>1984
            )
        ),
        2=>array(
            "Nome"=>"Mick",
            "Cognome"=>"Jagger",
            "Nato"=>array(
                "Giorno"=>13,
                "Mese"=>"Novembre",
                "Anno"=>1984
            )
        )
     ),
    "Amici"=>array(
        0=>array(
            "Nome"=>"Michael",
            "Cognome"=>"Myers",
            "Nato"=>array(
                "Giorno"=>8,
                "Mese"=>"Dicembre",
                "Anno"=>1986
            )
        ),
        1=>array(
            "Nome"=>"Jim",
            "Cognome"=>"Fear",
            "Nato"=>array(
                "Giorno"=>4,
                "Mese"=>"Febbraio",
                "Anno"=>1985
            )
        )
    )
);
echo json_encode($lista);
?>

I want to load this through Ajax: to do this, i wrote some JQuery code:

var output ="<ul>";
$.ajax({
    url:"lista.php",
    dataType:"json"
}).done(function(data) {
    $.each(data.Conoscenti, function() {
        output +="<li>"+this.Nome+" "+this.Cognome+" è un mio conoscente. &Egrave; nato il "+this.Nato.Giorno+" "+this.Nato.Mese+" "+this.Nato.Anno+"</li>";
    });
});
output += "</ul>";
$("body").html(output);

But the html page shows blank, without even errors. Analyzing the source code it shows only the ul tag, since it's out of the ajax call. Anyone knows how can i fix it? Thank you.

4 Answers 4

1

The issue is because the promise is executed after the request completes. This means that $('body').html(output) has already been run before you loop over the returned JSON.

To solve this you need to execute all code which depends on the response within the done() handler. Try this:

$.ajax({
    url:"lista.php",
    dataType:"json"
}).done(function(data) {
    var output = "<ul>";
    $.each(data.Conoscenti, function() {
        output += "<li>" + this.Nome + " " + this.Cognome + " è un mio conoscente. &Egrave; nato il " + this.Nato.Giorno + " " + this.Nato.Mese + " " + this.Nato.Anno + "</li>";
    });
    output += "</ul>";
    $("body").html(output);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Another nice explaination: i was reading about that but interrupted. Now iknow why is it important and i'll read very carefully now. Thanks =)
0
    $.ajax({
        url:"lista.php",
        dataType:"json"
    })
    .done(function(data) {
        var output ="<ul>";
        $.each( data.Conoscenti, function() {
           output +="<li>"+ this.Nome+" "+this.Cognome+" è un mio conoscente. &Egrave; nato il "+ this.Nato.Giorno+" "+ this.Nato.Mese+" "+ this.Nato.Anno+"</li>";
        });
        output += "</ul>";
        $("body").html(output); 
    });

Comments

0

The problem occurs because the printing of the variable "out" occurs before the execution of the ajax request. You have to put the printing of the variable within the callback "done".

A good idea, then, would be to have always a branch "done" and a branch "fail" (possibly also the branch "always") so as to manage the success or failure of the request.

Try this:

$.ajax({
 url:"lista.php",
 dataType:"json"
})
.done(function(data) {
   var output = "<ul>";
   $.each(data.Conoscenti, function() {
      output += "<li>" + this.Nome + " " + this.Cognome + " è     un mio conoscente. &Egrave; nato il " + this.Nato.Giorno + " " + this.Nato.Mese + " " + this.Nato.Anno + "</li>";
});
output += "</ul>";
$("body").html(output);
})
.fail(function( jqXHR, textStatus, errorThrown ){
   /*manage your error*/
})
.always(function()){
  console.log("completed");
}

1 Comment

Nice...i suppose i did this because of something i still don't know even after working with JQuery for long time. Well, thank you :D
0

Daniele, the problem is that you insert the html out of the done function, please try with the code below:

var output ="<ul>";
$.ajax({
    url:"lista.php",
    dataType:"json"
}).done(function(data) {
    $.each(data.Conoscenti, function() {
        output +="<li>"+this.Nome+" "+this.Cognome+" è un mio conoscente. &Egrave; nato il "+this.Nato.Giorno+" "+this.Nato.Mese+" "+this.Nato.Anno+"</li>";
    });
    output += "</ul>";
    $("body").html(output);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.