0
<script type="text/javascript">
    function get_test_json(){
        var HTML = "";
        var ClientHTML = "";
        var nomor = 1;
        HTML = '<table data-role="table" id="table-custom-2" data-mode="columntoggle" class="ui-body-d ui-shadow table-stripe ui-responsive" data-column-btn-theme="a" data-column-btn-text="Pilih Kolom" data-column-popup-theme="a">';
        HTML += "<thead>";
        HTML += '<tr class="ui-bar-a">';
        HTML += '<th data-priority="2">No</th>';
        HTML += '<th data-priority="3">Foto</th>';
        HTML += '<th>Nama</th>';
        HTML += '<th data-priority="5">Keterangan</th>';
        HTML += '</tr>';
        HTML += '</thead>';
        HTML += '<tbody>';
        $.getJSON("http://localhost/JSON_test/my_json_list.php", function(person){
            $.each(person, function(key, value){
                $.each(value, function(names, specs){
                    ClientHTML += '<tr>'; 
                    ClientHTML += '<th>'+nomor+'</th>'; 
                    ClientHTML += '<td><img src="" alt="" height="" width=""></td>'; 
                    ClientHTML += '<td>'+names+'</td>'; 
                    ClientHTML += '<td><a href="http://localhost/4r.mobile.kemensos/profil_peneliti.html">'+specs+'</a></td>'; 
                    ClientHTML += '</tr>';
                    nomor = nomor+1;
                });
            });
        });
        alert(ClientHTML); // this is the problem
        HTML += ClientHTML;
        HTML += '</tbody>';
        HTML += '</table>';
        document.getElementById("test").innerHTML = HTML;
    }
    </script>

the ClientHTML variable is null when i remove the alert.
i'm sorry for my bad english and some dump code, i'm newbie here
thanks for your help

  • Very common problem: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs Jul 17 '13 at 04:27
  • See this answer: http://stackoverflow.com/questions/13306067/javascript-variable-empty-without-previous-alert-messge – Ryan.B Jul 17 '13 at 04:31

2 Answers2

1

That is a not problem with alert and non alert----its matter of time.

 $.getJSON("http://localhost/JSON_test/my_json_list.php", function(person){
            $.each(person, function(key, value){
                $.each(value, function(names, specs){
                    ClientHTML += '<tr>'; 
                    ClientHTML += '<th>'+nomor+'</th>'; 
                    ClientHTML += '<td><img src="" alt="" height="" width=""></td>'; 
                    ClientHTML += '<td>'+names+'</td>'; 
                    ClientHTML += '<td><a href="http://localhost/4r.mobile.kemensos/profil_peneliti.html">'+specs+'</a></td>'; 
                    ClientHTML += '</tr>';
                    nomor = nomor+1;
                     HTML += ClientHTML;
                   HTML += '</tbody>';
                   HTML += '</table>';
                  document.getElementById("test").innerHTML = HTML;
                });
        });
    });

you have todo your operations after you complete the ajax request .

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

I believe this will work. Just seperate them into two functions.

<script>
function get_test_json(){
  var nomor = 1;
  var ClientHTML = "";
  $.getJSON("http://localhost/JSON_test/my_json_list.php", function(person){

        $.each(person, function(key, value){
            $.each(value, function(names, specs){
                ClientHTML += '<tr>'; 
                ClientHTML += '<th>'+nomor+'</th>'; 
                ClientHTML += '<td><img src="" alt="" height="" width=""></td>'; 
                ClientHTML += '<td>'+names+'</td>'; 
                ClientHTML += '<td><a href="http://localhost/4r.mobile.kemensos/profil_peneliti.html">'+specs+'</a></td>'; 
                ClientHTML += '</tr>';
                nomor++;
            });
        });


        get_next(ClientHTML);
    });


}

function get_next(ClientHTML){
    var HTML = "";
    HTML += '<table data-role="table" id="table-custom-2" data-mode="columntoggle" class="ui-body-d ui-shadow table-stripe ui-responsive" data-column-btn-theme="a" data-column-btn-text="Pilih Kolom" data-column-popup-theme="a">';
    HTML += "<thead>";
    HTML += '<tr class="ui-bar-a">';
    HTML += '<th data-priority="2">No</th>';
    HTML += '<th data-priority="3">Foto</th>';
    HTML += '<th>Nama</th>';
    HTML += '<th data-priority="5">Keterangan</th>';
    HTML += '</tr>';
    HTML += '</thead>';
    HTML += '<tbody>';

    HTML += ClientHTML;
    HTML += '</tbody>';
    HTML += '</table>';
    document.getElementById("test").innerHTML = HTML;

    $('#table-custom-2').table('refresh');
}
</script>
James
  • 1,562
  • 15
  • 23