如何通过$ .ajax将多个数据发布到Codeigniter控制器方法

时间:2012-02-23 06:26:13

标签: ajax codeigniter

这是我想向其发送数据的方法

function get_lesson($reshte,$poodeman){
        $this->load->model('dropdown_model');
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($this->dropdown_model->get_lessons($reshte,$poodeman)));
    }

这是模型文件中的get_lessens()函数。

  function get_lessons($reshte = null, $poodeman=NULL){
          $this->db->select('rlessid, title');

          if($reshte != NULL AND $poodeman!= NULL ){
              $this->db->where('reshteid', $reshte);
              $this->db->where('poodemanid', $poodeman);
          }

          $query = $this->db->get('tbllessons_root');

          $lessons = array();

          if($query->result()){
              foreach ($query->result() as $lesson) {
                  $lessons[$lesson->rlessid] = $lesson->title;
              }

          return $lessons;
          }else{
              return FALSE;
          }
    }

这是我在视图文件中的ajax调用

var reshteid = $('#reshte').val();
 var poodemanid = $('#poodemanha').val();

$.ajax({
     type:"POST",
     url:"http://localhost/crud-grocery/index.php/examples/get_lesson/",//+reshte+"/"+poodeman,
     data: "reshte="+reshteid+"&poodeman="+poodemanid,
     success: function(lessons)
     {
        $.each(lessons,function(rlessid,title){
          var opt = $('<option />');
        opt.val(rlessid);
        opt.text(title);
        $('#lessons').append(opt);
        });
    }

});

如您所见,我正在尝试在表单中链接选项 但是当我尝试将两个参数发送(发送)到控制器方法时,问题出现了 任何想法?

2 个答案:

答案 0 :(得分:1)

在您的控制器中,您需要获取 POST 值而不是参数:


//in controller
function get_lessons(){
...
//get POST values
$reshte = $this->input->post('reshte');
$poodeman = $this->input->post('poodeman');

答案 1 :(得分:0)

您必须将数据作为javascript对象文字传递:

...
data: {
  reshte: reshteid,
  poodeman: poodemanid
}
....