Error sending data by ajax to the controller

0

My function called register does not send the data to the controller even though everything is fine, it only returns error, but how is it possible? if in the condition I say, if the field is empty I return error, but it is not empty I should return the message "success", What am I doing wrong?

$(function () {
    $("#btn-registrar").on('click',  function(event) {
        event.preventDefault();
        var _form = $("#form-Registrar");
        if (_form.find('#nombres').val()==='') {
            return false;
        };

        $.ajax({
            url: baseURL +'usuario/registrar',
            type: 'POST',
            dataType: 'json',
            data: _form.serializeArray(),
        }) .done(function(response) {
            if (Boolean(response.status)===true) {
                //loadURL(baseURL+'marcas/list_marcas');
                alert("exito");
            }else{
                alert(response.message);
            }
        }).fail(function(response) {
            console.log("error",response);
        })
    });
});

Here the controller:

<?php class Usuario extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('Model_Usuario');
    }

    public function index(){
        $data['contenido']="usuario/index";
        //$data['contenido']="usuario/index";//enviar a plantilla la vista
        $data['selPerfil']=$this->Model_Usuario->selPerfil();
        $this->load->view("plantilla",$data);//cargamos la plantilla
    }

    public function registrar(){
         echo "sadsa";
    }
}?>

Here the views:

<h1>FORMULARIO DE REGISTRO</h1>
<form id="form-Registrar">
    <div class="form-group">
        <label for="exampleInputPassword1">Nombres</label>
        <input type="text" class="form-control" id="nombres" placeholder="Nombres">
    </div>
    <button id="btn-registrar" type="submit" name="werwrw" class="btn btn-default">Registrar</button>
</form>
    
asked by Kpeski2814 27.08.2018 в 18:32
source

2 answers

0

I think the error is because you do not have the file extension, try this:

url: baseURL +'usuario/registrar.php',

I hope you serve, you tell me ...

    
answered by 27.08.2018 в 18:55
0

After evaluating and analyzing the code with the var_dump the problem was that when using the jquery ajax and specifying the url parameter, which will have our controller destination, this driver must be implemented, since ajax establishes a bidirectional communication with this and the other was that I was not collecting the values properly in the view, I publish the code working at 100%

$(function() {

  $("#btn-registrar").on('click',  function(event) {
    event.preventDefault();

    var _form = $("#form-Registrar");

    if (_form.find('#id').val()==='') {
      alert("Ingrese id");
  
        return false;
    };
    
    if (_form.find('#nombres').val()==='') {
      alert("Ingrese Nombre");

        return false;
    };

  
    $.ajax({
      url: baseURL +'usuario/registrar',
      type: 'POST',
      dataType: 'json',
      data: _form.serializeArray(),
    })
    .done(function(response) {

      if (Boolean(response.status)===true) {
      //  loadURL(baseURL+'marcas/list_marcas');
        alert("exito");
      }else{
        alert(response.message);

      }
    })
    .fail(function(response) {
      console.log("error",response);
    })


  });



	});

<h1>FORMULARIO DE REGISTRO</h1>
<form id="form-Registrar">

  <div class="form-group">
    <label for="exampleInputPassword1">id</label>
    <input type="text" class="form-control"  name="id" placeholder="id">
  </div>

  <div class="form-group">
    <label for="exampleInputPassword1">Nombres</label>
    <input type="text" class="form-control"  name ="nombres" placeholder="Nombres">
  </div>

  <button id="btn-registrar"  name="werwrw" class="btn btn-default">Registrar</button>
</form>

This is the Controller

  

function __construct () {     parent :: __ construct ();

  $this->load->model('Model_Usuario');

}

public function index () {

$data['contenido']="usuario/index";
//  $data['contenido']="usuario/index";//enviar a plantilla la vista
  $data['selPerfil']=$this->Model_Usuario->selPerfil();

  $this->load->view("plantilla",$data);//cargamos la plantilla

}

public function register () {

$_name = $this->input->post('nombres',true);

        if ($_name==='')
             exit(json_encode(array('status'=>false,'id'=>'nombres')));

$_des = $this->input->post('id',true);
                     if ($_des==='')
                          exit(json_encode(array('status'=>false,'id'=>'id')));

$_input = $this->input->post();

        if ($this->Model_Usuario->addMarca( $_input ))

            exit(json_encode(array('status'=>true,'message'=>'Marca registrada')));
        else
            exit(json_encode(array('status'=>false,'message'=>'Error al  registrar')));

}

}

? >

This is the Model

  

function __construct () {       parent :: __ construct ();

    $this->load->database();

}

public function selPerfil(){
  $query="select * from perfil ";
  $result = $this->db->query($query);
  return $result->result();

}

public function addMarca ($ _input) {     $ data = array (       'per_id' = > $ _ input ['id'],        'per_name' = > $ _ input ['names'],

    );

if ( $this->db->insert('perfil',$datos))
    return true;


return false;

}

}

    
answered by 27.08.2018 в 22:56