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();
}
}
}
コントローラの構造を教えてもらえますか? – shantanu
それは....上にあります –
コントローラまたはajax上の{ – Poria