简体   繁体   中英

How to append the JSON data to HTML in JavaScript?

I get some data using JSON array. I want to append each data in a div. But I don't get what's wrong in that?

controller

   function get_performers()
   {
    $id = $this->input->post('id'); 
    $exam = $this->input->post('exam'); 
    $datas=$this->job->get_top_ten_st_data($id,$exam);
    $out['student_details'] = $datas;
     echo json_encode($out); 
   }

script

function get_performers(id,exam)
{

    $.ajax({
        url:"<? echo base_url();?>class_analysis/get_performers", 
        dataType: 'json',
        type: "POST",
        data: {id:id,exam:exam},      
        success:function(result) {  
            // alert("haii");
            console.log(result);
            result = JSON.parse(result);

          var tab= "<div class='col-xs-2 blk-ht'>  <span class='hd'>Names</span> </div>";
           for(var i=0;i<result.student_details.length;i++)
            {

                 tab=tab+"<div class='col-ds-1'><span class='subjNames'>" + result.student_details[i]["subject_name"]+ "</span></div> ";
            }   

           jQuery("#subjectNames").append(tab);

        }
    }); 
}

Any problem in this?

Try html not append

  jQuery("#subjectNames").html(tab);

Also if jQuery("#subjectNames") equal with null in your console this mean that you don't have element with id="subjectNames" in html not id="#subjectNames" or other. May be you use classes then try $(".subjectNames") with . not #

The Loop should work... Seems to be another problem with your result.

 var tab= "<div class='col-xs-2 blk-ht'><span class='hd'>Names</span> </div>"; for(var i=0;i<20;i++) { tab=tab+"<div class='col-ds-1'><span class='subjNames'>" + "test: " + i + "</span></div> "; } jQuery("#subjectNames").append(tab); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="subjectNames"><div> 

dataType: 'json' in $.ajax options - automaticaly parse your json

and USE JQUERY )))

IN SUCCES AJAX FUNCTION

$.each( result.student_details, function( key, value ) {
      alert( key + ": " + value );
});
<?php 
function get_performers()
{
    $id = $this->input->post('id'); 
    $exam = $this->input->post('exam'); 
    $datas=$this->job->get_top_ten_st_data($id,$exam);
    $out['student_details'] = $datas;
    echo json_encode($out); 
}
?>
<script>
$.ajax({
    url:"<? echo base_url();?>class_analysis/get_performers", 
    dataType: 'json',
    type: "POST",
    data: {id:id,exam:exam},      
    success:function(result) {

        $.each(response.student_details, function(key,value){
             appendHtml(key,value);
        });


    }
}); 

function appendHtml(key,value)
{
    var tab= "<div class='col-xs-2 blk-ht'>  <span class='hd'>Names</span> </div>";
    tab = tab+"<div class='col-ds-1'><span class='subjNames'>" +value+ "</span></div> ";
    jQuery("#subjectNames").append(tab);
}

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM