【问题标题】:Ajax response arrayAjax 响应数组
【发布时间】:2015-02-15 20:47:08
【问题描述】:

我想将 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
    )

)

有人可以帮我解决这个问题

【问题讨论】:

  • 你的问题在我看来有点含糊,你应该多解释一下。
  • 您确定网址正确吗? ajax 调用中的data: std_data 是什么?
  • data:std_code 作为 's-14-1' 发送
  • 在检查萤火虫时,我看到响应为 []
  • 哟,有什么帮助吗?

标签: javascript php jquery arrays ajax


【解决方案1】:
var objectRet= jQuery.parseJSON(response);
for(var i=0;i<objectRet.length;++i) {
   alert(objectRet[i].class_id)
}

【讨论】:

  • 我的代码中没有使用 json,在这种情况下可以使用吗?
【解决方案2】:
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')));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2016-11-28
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多