Ajax + Codeigniter returning "error"

Asked

Viewed 53 times

-1

so I’m trying to perform a request in Ajax to save a registration but it’s not working. I’m using Codeigniter. Every time I try to send the Form ajax is returning me the error I don’t know where the error might be

$('#enviarAltera').on('click', function(){
    $.ajax({
        url: base_url + '/index.php/contatos/salvarAlterar', 
        type: 'POST',
        data: $('#form').serialize(),
        dataType: 'json',
        success: function(data){
            if(data.status)
            {
                alert('O aluno foi inserido corretamente!');
            }
        },
        error: function(){
            alert('Ocorreu um erro ao tentar salvar o aluno.');
        }
    });
});
    public function alterar($identificador)
    {
        $this->load->model("Contatos_Model");

        $contato = $this->Contatos_Model->buscarId($identificador);
        $pacote = array(
            "contato" => $contato,
            "pagina" => "contatoAlterar.php"
        );

        $this->load->view('index', $pacote);
    }

    //controller
    public function salvarAlterar()
    {
        $id = $_POST['id'];
        $nome = $_POST['nome'];
        $telefone = $_POST['telefone'];
        $email = $_POST['email'];

        $this->load->model("Contatos_Model");
        $this->Contatos_Model->salvarAlterar($id, $nome, $telefone, $email);

        redirect("contatos");
    }
<div class="col-md-12 col-sm-12 col-xs-12">
    <div class="x_panel">
        <div class="x_title">
            <h2>Atualiza Contatos</h2>
            <div class="clearfix"></div>
        </div>
        <div class="x_content">
            <br>
            <form name='form' id="form" class="form-horizontal form-label-left" method="POST">
                <input type='hidden' name='id' value='<?php echo $contato[0]->id; ?>' />
                <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">Nome: <span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                        <input type='text' id="nome" name='nome' value=<?php echo $contato[0]->nome; ?> id="first-name" class="form-control col-md-7 col-xs-12 ">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">E-mail: <span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                        <input type='text' id="email" name='email' value=<?php echo $contato[0]->email; ?> required="required" class="form-control col-md-7 col-xs-12 ">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">Telefone:</label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                        <input data-mask="(00) 00000-0000" id="telefone" class="form-control col-md-7 col-xs-12" type='text' name='telefone' value="<?php echo $contato[0]->telefone; ?>">
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                        <input id="enviarAltera" type="submit" class="btn btn-success" value="Alterar" name="enviarform">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

  • If you read the documentation of callback error you will see that this callback accepts up to three parameters Function( jqXHR jqXHR, String textStatus, String errorThrown ) provide these parameters print them and show us the actual error status.

1 answer

0


I believe that the best strategy is to separate the backend from the frontend at first. You’re trying to treat your backend as a Rest API but it’s not (I’ve found that you redirect to a listing after you register). My suggestion is that you treat the '/contacts/saves' route as a Rest endpoint and work with HTTP status to return success or error, using an application to test (a Postman for example). There is a lot of content on the web talking about Rest, it is worth a read in some articles. When your backend is working and returning the status correctly, you go to the frontent. Alias, you can get the errors through the error callback you set, just put the function parameter and use it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.