2016-05-08 8 views
1

は、私はまた、私はシンプルなAJAX呼び出しによって404codeigniterのajaxで私のコントローラにアクセスするにはどうしたらいいですか?コールは一度

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

コントローラまたはajax上の{ – Poria

答えて

0

スタートをエラー与え、SITE_URL、BASE_URL、直接URL何も変更を使用しよう、とされていますモデルと他のコントローラロジックを呼び出す作業がうまくいきましたので、私はid="test_btn"でビューにボタンを作成してからこのコードを使用してください。

$("#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」

アラートを得れば、そしてあなたの追加を開始他のロジックは、一度に1ステップずつ進んでください。また、あなたがコールエラーと成功したコールを表示するのに役立つブラウザコンソール(F12)を使用して

希望に役立つ希望!

関連する問題