Ajax响应数组

时间:2014-12-17 10:22:20

标签: javascript php jquery arrays ajax

我想在表单选择输入中插入ajax响应数组。为了这个目的,我想看看数组。这是我的功能;

function _get_student_failed_classes()
{
$failed_classes=$this->db->select('class_student.class_id,course.course_code,course.course_name,course.course_credit')
        ->join('class','class.class_id=class_student.class_id','LEFT')
        ->join('course','course.course_id=class.class_course','LEFT')
        ->where('class.class_status',$active)
        ->where('class_student.class_marks <=',50)
        ->where('class_student.student_id',$std_code)
        ->order_by('class.class_id')
        ->get('class_student')->result();
echo $failed_classes;
}

这是我在形式上的ajax调用

$.ajax({
        url: '<?php echo base_url()."index.php/student/get_student_failed_classes/s-14-1"; ?>',
        type: 'POST',   data: std_code,
        success: function(response)
        {

          alert(response);

        },
        error: function()
        {
          alert(error);
        }

我的反应是[]

实际数组看起来像这样

Array
(
[0] => stdClass Object
    (
        [class_id] => 3
        [course_code] => cs3
        [course_name] => cs3
        [course_credit] => 3
    )

[1] => stdClass Object
    (
        [class_id] => 4
        [course_code] => cs4
        [course_name] => cs4
        [course_credit] => 4
    )

[2] => stdClass Object
    (
        [class_id] => 5
        [course_code] => cs5
        [course_name] => cs5
        [course_credit] => 3
    )

)

可以帮助我解决这个问题

2 个答案:

答案 0 :(得分:0)

var objectRet= jQuery.parseJSON(response);
for(var i=0;i<objectRet.length;++i) {
   alert(objectRet[i].class_id)
}

答案 1 :(得分:0)

success: function(response) {
   var $select = $('<select id="mySelect"/>');

   var $options = response.map(function(a, i) {
       return $('<option />', {
                  value: a.class_id,
                  text: a.course_code + ' - ' + 
                        a.course_name + ' (' + a.course_credit +')'
       });
   });

   $options.appendTo($select.appendTo($('#someForm')));
}