Error returning a data from Controller php to an Ajax in jQuery

0

I have a problem returning a data from Controllers/registro in php to% Ajax . The controller receives a data from Model/registro.php , but it does not return the Ajax that data it receives.

What could be the reason why Controller/registro.php does not return any data to Ajax , even though it does insert in Model/registro.php ?

Model / registro.php

public function registrar($username, $email, $password, $confirm_password)
{                                 
    try
    { 
        $this->has = crypt($password);
        $this->has_con = crypt($confirm_password);
        $p= "soy diferente de uno";

        $registro = $this->conexion->conexion->query("insert into registro_por (nombre, correo, pass, conf_pass) values( '$username', '$email', '$this->has', '$this->has_con')");

        if ($registro)
        {
            return $p;                            
        }
        else
        {
            throw new Exception("Fallos la ejecución");                     
        }


    }
    catch (Exception $e)
    {
        echo 'Message: '.$e->getMessage();
    }         
}

Controllers / registro.php

require_once("../Model/registro.php");

$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$confirm_password = $_POST["confirm_password"];      

$reg = new Registro();
$t = $reg->registrar($username, $email, $password, $confirm_password);  

var_dump($t);

if ($t != 1)
{
    //return "ok";
    //echo "es difenrete de uno";
    return $t;
}
else
{
    return $t;
    //echo "fail";
}

JavaScript

Ajax jQuery receives the data from the controller:

$('#register-submit').click(function () {      

    var data = $('#register-form').serialize();

    $.ajax({
        beforeSend: function () {
            console.log(data);
        },
        type: 'POST',
        url: '../Controllers/registro.php',
        data: data,
        success: function (data) {
            console.log('nanananana');
            console.log("soy data"+data);
        }
    });

});
    
asked by Corovino 07.02.2016 в 22:29
source

2 answers

0

return in your "Controllers / registro.php" returns to the line of code from where it was called; this is: never leaves PHP.

If you want that what generates a PHP code is returned to a call by AJAX you will have to do it as if you were going to paint it in the browser, for example echo $t instead of return $t .

    
answered by 07.07.2016 в 10:32
0

Do not blindly program when you use Ajax . In your code you are not adding a% error% co or an upload to your log function. To return the error from jQuery add this:

error: function (xhr) {
    console.log(xhr.responseText);
}

And do it like this:

$.ajax({
    beforeSend: function () {
        console.log(data);
    },
    type: 'POST',
    url: '../Controllers/registro.php',
    data: data,
    //Cargando, le agregas un gif a un div
    beforeSend: function () {
        console.log("cargando")
    },
    success: function (data) {
        console.log('nanananana');
        console.log("soy data"+data);
    },
    //Error que mostrará desde PHP
    error: function (xhr) {
        console.log(xhr.responseText);
    }
});
    
answered by 08.02.2016 в 05:40