使用ajax和jquery异步查询数据库,并将结果发布回codeigniter View

时间:2015-04-03 19:51:21

标签: php jquery ajax codeigniter

我已经很多了。

我想在CI模型中发送ID并通过CI控制器获取返回值

我的观点是

      <script type="text/javascript">
             function showsome(){
                 var rs = $("#s_t_item option:selected").val();
                 var controller = 'items';
                 var base_url = '<?php echo site_url(); ?>';

                 $.ajax({
                         url : base_url+ '/' + controller+ '/get_unit_item',
                     type:'POST',
                     contentType: 'json',
                     data: {item_id: rs},
                     success: function(output_string){
                         //$('#result_area').val(output_string);
                         alert(output_string);
                     }
                 });

             }

         </script>

我的控制器方法是

    public function get_unit_item()
      {
        $received = $this->input->post('item_id');  

        $query = $this->units_model->get_unit_item($received);
        $output_string = '';
        if(!is_null($query)) {
        $output_string .= "{$query}";
        } else {
        $output_string = 'There are no unit found';
        }
        echo json_encode($output_string); 
}

我的模特职能负责

public function get_unit_item($where){
    $this->db->where('item_id',$where);
    $result = $this->db->get($this->tablename);
    if($result->num_rows()  >0 ){
        $j = $result->row();
        return $j->unit_item_info ; 
    }
}

Html代码

   <?php $id = 'id="s_t_product" onChange="showsome();"';
        echo form_dropdown('product_id[]', $products, $prod,$id); ?>

我试图只使用id,但未能触发,因此传递函数onchange似乎选择项目并开火

使用firebug我可以看到post请求发送item_id = 2但响应长度为0并且使用php结果代码302

POST enter image description here 响应 enter image description here

我怎样才能实现这个目标?(模型加载在构造函数中)

2 个答案:

答案 0 :(得分:1)

轻轻改变你的控制器和型号:

// Model 
public function get_unit_item($where){
    $this->db->where('item_id',$where);
    $result = $this->db->get($this->tablename);

    if($result->num_rows()  > 0 ) {
        $j = $result->row();
        return $j->unit_item_info ; 
    }
    else return false;
}


// Controller 
public function get_unit_item()
  {
    $received = $this->input->post('item_id');  
    $return = array('status'=>false);

    if( $query = $this->units_model->get_unit_item($received) ) {
        $return['status'] = true;
        // Add more data to $return array if you want to send to ajax
    }


    $this->output->set_content_type("application/json")
                 ->set_output(json_encode($return));
}

检查JavaScript中的返回值:

$.ajax({
     url : base_url+ '/' + controller+ '/get_unit_item',
     type:'POST',
     dataType: 'json',
     data: {item_id: rs},
     success: function( response ){
           if( response.status === true ) {
               alert('Everything Working Fine!');
               console.log( response );
           }
           else alert('Something went wrong in query!');
     }
 });

答案 1 :(得分:0)

在尝试了各种方法后,我终于找到了真正的问题,我认为这可能是所有发现302错误的问题。在这个项目(服务器)中,同一根中有两个系统,每个系统都有自己的codeigniter文件。如上所示,我正在使用

             var controller = 'items';
             var base_url = '<?php echo site_url(); ?>';
             url : base_url+ '/' + controller+ '/get_unit_item',

作为url的值但是我试图从基础中放入完整的url并且它工作现在它是

      url: '<?php echo base_url(); ?>index.php/en/items/get_unit_item',

。我认为任何一个有重定向问题的人首先要检查的是