I have been trying to use site_url,base_url, url directly but nothing change, also gives me error 404
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);
}
});
}
controller
<?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();
}
}
}
start by a simple ajax call, and once the call is working fine start calling the model and the other controller logic, so I would suggest you to make a button on the view with an id="test_btn"
and then in the javascript use this code
$("#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;
});
and then in the empleados
controller just read the data and return it to the view like so
public function mostrar()
{
$buscar = $this->input->post('buscar',TRUE);
echo($buscar);
}
then you should get an alert "123"
once you get the alert , then start adding your other logic , just go one step at a time . also use you browser console (F12) that will help you see the call errors and successful calls
hope that helps !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.