2016-05-08 78 views
1

我一直在嘗試使用SITE_URL,BASE_URL,直接鏈接但沒有什麼變化,也給了我一個簡單的Ajax調用錯誤404我怎樣才能訪問我的控制器在ajax codeigniter?

function mostrarDatos(valor){ 
    $.ajax({ 
     url:"http://localhost/empresa/empleados/mostrar", 
     type:"POST", 
     data:{buscar:valor}, 
     success:function(respuesta){ 
      //alert(respuesta); 
      var registros = eval(respuesta); 

      html ="<table class='table table-responsive table-bordered'><thead>"; 
      html +="<tr><th>#</th><th>Nombres</th><th>Apellidos</th><th>DNI</th><th>Telefono</th><th>Email</th><th>Accion</th></tr>"; 
      html +="</thead><tbody>"; 
      for (var i = 0; i < registros.length; i++) { 
       html +="<tr><td>"+registros[i]["id_empleado"]+"</td><td>"+registros[i]["nombres_empleado"]+"</td><td>"+registros[i]["apellidos_empleado"]+"</td><td>"+registros[i]["dni_empleado"]+"</td><td>"+registros[i]["telefono_empleado"]+"</td><td>"+registros[i]["email_empleado"]+"</td><td><a href='"+registros[i]["id_empleado"]+"' class='btn btn-warning' data-toggle='modal' data-target='#myModal'>E</a> <button class='btn btn-danger' type='button' value='"+registros[i]["id_empleado"]+"'>X</button></td></tr>"; 
      }; 
      html +="</tbody></table>"; 
      $("#listaEmpleados").html(html); 
     } 
    }); 
} 

控制器

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Empleados extends CI_Controller 
{ 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model("Empleados_model"); 
    } 

    public function index() 
    { 
     $this->load->view('frontend/empleados'); 
    } 

    public function guardar() 
    { 
     //El metodo is_ajax_request() de la libreria input permite verificar 
     //si se esta accediendo mediante el metodo AJAX 
     if ($this->input->is_ajax_request()) 
     { 
      $nombres = $this->input->post("nombres"); 
      $apellidos = $this->input->post("apellidos"); 
      $dni = $this->input->post("dni"); 
      $telefono = $this->input->post("telefono"); 
      $email = $this->input->post("email"); 

      $datos = array(
       "nombres_empleado" => $nombres, 
       "apellidos_empleado" => $apellidos, 
       "dni_empleado" => $dni, 
       "telefono_empleado" => $telefono, 
       "email_empleado" => $email 
      ); 
      if($this->Empleados_model->guardar($datos)==true) 
      { 
       echo "Registro Guardado"; 
      } 
      else 
      { 
       echo "No se pudo guardar los datos"; 
      } 
     } 
     else 
     { 
      show_404(); 
     } 
    } 

    public function mostrar() 
    { 
     if ($this->input->is_ajax_request()) 
     { 
      $buscar = $this->input->post("buscar"); 
      $datos = $this->Empleados_model->mostrar($buscar); 
      echo json_encode($datos); 
     } 
     else 
     { 
      show_404(); 
     } 
    } 

    public function actualizar() 
    { 
     if ($this->input->is_ajax_request()) 
     { 
      $idsele = $this->input->post("idsele"); 
      $nombres = $this->input->post("nombressele"); 
      $apellidos = $this->input->post("apellidossele"); 
      $dni = $this->input->post("dnisele"); 
      $telefono = $this->input->post("telefonosele"); 
      $email = $this->input->post("emailsele"); 
      $datos = array(
       "nombres_empleado" => $nombres, 
       "apellidos_empleado" => $apellidos, 
       "dni_empleado" => $dni, 
       "telefono_empleado" => $telefono, 
       "email_empleado" => $email 
      ); 
      if($this->Empleados_model->actualizar($idsele,$datos) == true) 
      { 
       echo "Registro Actualizado"; 
      } 
      else 
      { 
       echo "No se pudo actualizar los datos"; 
      } 
     } 
     else 
     { 
      show_404(); 
     } 
    } 

    public function eliminar() 
    { 
     if ($this->input->is_ajax_request()) 
     { 
      $idsele = $this->input->post("id"); 
      if($this->Empleados_model->eliminar($idsele) == true) 
      { 
       echo "Registro Eliminado"; 
      } 
      else 
      { 
       echo "No se pudo eliminar los datos"; 
      } 
     } 
     else 
     { 
      show_404(); 
     } 
    } 

} 
+0

你能給你的控制器的結構嗎? – shantanu

+0

它是以上.... –

+0

嘗試改變這個if($ this-> input-> is_ajax_request()){if($ this-> input-> post(「buscar」)){ – Poria

回答

0

開始,一旦通話做工精細開始調用模型和其他控制器邏輯,所以我建議你做一個按鈕,在視圖上與id="test_btn"然後在JavaScript中使用此代碼

$("#test_btn").click(function() 
    { 

     var target_url = '<?php echo(base_url()."empleados/mostrar") ; ?>'; 
     var data_to_send = {buscar:"123"}; 

     $.ajax(
     { 
      url : target_url, 
      data: data_to_send, 
      type: "POST", 
      success: function(return_data) 
      { 
       alert(return_data) 

      }, 
      error: function(jqXHR, textStatus, errorThrown) 
      { 
       console.log('textStatus='+textStatus); 
       console.log('errorThrown='+errorThrown); 
      } 
     }); 

     // prevent default 
     return false; 
    }); 

然後在empleados控制器只是讀取數據,並將其返回到視圖像這樣

public function mostrar() 
    { 
     $buscar = $this->input->post('buscar',TRUE); 

     echo($buscar); 
    } 

那麼你應該得到一個警告「123」

,一旦你得到警報,然後開始加入您的其他邏輯,只需一次一步。也可以使用你的瀏覽器控制檯(F12),這將幫助你看到通話錯誤和成功通話

希望有幫助!