I cannot enter the Success json function

Asked

Viewed 59 times

0

Dai galera.... I am unable to pass the return of my code to the function Success of my ajax via json.... I am using codeiginiter, php, mysql.... someone can help me.... it’s been almost a week that I’m trying and I don’t unpack...

My controller....

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct() {
    parent::__construct();
}
public function index() {
    $dados['tituloPrincipal'] = 'Login';
    $this->load->view('Listar/login', $dados);
}
public function cadastroLogin() {
    $dados['nomeCliente'] = $this->input->post('nomeCliente');
    $dados['telefoneCliente'] = $this->input->post('telefoneCliente');
    $dados['senhaCliente'] = $this->input->post('senhaCliente');
    $dados['emailCliente'] = $this->input->post('emailCliente');
    $this->load->model("mLogin");
    $result = $this->mLogin->cadastrandoLogin($dados);
    $msg['success'] = false;
    if ($result) {
        $msg['success'] = true;
    }
    echo json_encode($msg);
}

}

My Model....

public function __construct()
{
    parent::__construct();
    $this->load->database();
}
public function cadastrandoLogin($dados = null) {
    if ($dados != null) {            
        $this->db->insert('clientes', $dados);
    }
    try {
        return true;
    } catch (Exception $exc) {
        return false;
    }
}

}

My script.....

$(function () {
$('#adicionarCliente').click(function () {
    var data = $('#cadastroCliente').serialize();
    if ((nomeCliente && emailCliente && telefoneCliente && senhaCliente) != null) {
        $.ajax({
            method: 'POST',
            url: '<?php echo base_url() ?>Login/cadastroLogin',
            data: data, //Dados
            async: false,
            dataType: 'json',
            success: function (response) {
                if (response.success) {
                    alert(response);
                    //ajax_redirect('<?php echo base_url() ?>Login');
                }
                else
                {
                    alert("Lasco");
                }
            },
            error: function () {
                alert('Lascado');
            }
        });
    }
});
});

  • 1

    Lascô nay Lasco; lascô is in the past perfect (despite being a less formal use with orality marks) and indicates unpleasant surprise, already lasco is in the present and indicates the habit of chipping someone, as if it were a threat or hobby

  • I believe that if you put the method complete you get more information for debugging, like suddenly error 500 on the server side; see http://api.jquery.com/jquery.ajax/

  • Thanks for the return, but I did not understand this complete method...can give me an example??

  • complete Type: Function( jqXHR jqXHR, String textStatus ) A Function to be called when the request finishes (after Success and error callbacks are executed). The Function gets passed two Arguments: The jqXHR (in jQuery 1.4.x, Xmlhttprequest) Object and a string categorizing the status of the request ("Success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete Setting can Accept an array of functions. Each Function will be called in turn. This is an Ajax Event. (From the linked documentation)

  • You use as the success, but ensures that it always runs after the end of the server return. The success is executed only in the event of success

No answers

Browser other questions tagged

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