Ajax does not return the request date

3

I run a query with ajax on a php page, the problem is that even asking to return data, the ajax 'date' does not return anything, I give an alert on the 'date' and it returns me a blank alert, JS below:

$(function(){

    $(".logando").click(function(event){
        event.preventDefault();

        if($("#email").val() == "" || $("#senha").val() == "") {
            $(".obrigatorio").slideDown(500).css("display","block");
        }
        else {

            var emailUsuario =  $("#email").val();
            var senhaUsuario =  $("#senha").val();

            $.ajax({
                type: "POST",
                url:  "../../controller/Logar_Cadastrar.inc.php",
                data: {email: emailUsuario, senha: senhaUsuario},
                contentType: "application/json; charset=utf-8",
                dataType: "json",

                beforeSend: function() {
                    $(".obrigatorio").slideDown(100).html("Carregando");
                },

                success: function(data) {
                    alert(data);
                }
            })
        }
    })

})

Login block code HTML page:

<form action="../../controller/Logar_Cadastrar.inc.php" method="post">
      <input type="text" name="email" required placeholder="Digite seu e-mail..." id="email"><br>
      <input type="password" name="senha" required placeholder="Digite sua senha..." id="senha"><br>
      <input type="hidden" name="logar">
      <input type="submit" value="Logar-se" class="logando"><br>
</form>

PHP Code:

<?php

require_once('../model/Logar_Cadastrar.class.php');
$logarCadastrar = new Logar;

//Função para logar
if(isset($_POST['logar'])):
    $email = trim(strip_tags($_POST['email']));
    $senha = trim(strip_tags($_POST['senha']));

    $verificar = $logarCadastrar->Consulta("SELECT * FROM CS_usuarios WHERE email = ? AND senha = ?","ss","{$email}","{$senha}");
    if($verificar >= 1):
        // return "Encontrado";
        echo "Encontrado";
    else:
        // return "Não encontrado";
        echo "Não encontrado";
    endif;
endif;
    
asked by anonymous 06.09.2016 / 21:08

2 answers

0

In order for you to receive the date in json it is necessary for you to return a PHP JSON so that it arrives in your Ajax success method, so much so that if you run the ajax error function, you will receive a message from error:

$.ajax({ type: "POST",
            url:  "../../controller/Logar_Cadastrar.inc.php",
            data: {email: emailUsuario, senha: senhaUsuario},
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            beforeSend: function() {
                $(".obrigatorio").slideDown(100).html("Carregando");
            },

            success: function(data) {
                alert(data);
            }, error: function(data){ console.log(data); });

In order for your code to work, add json_encode to your line that is printing the Found. As follows:

require_once('../model/Logar_Cadastrar.class.php');
$logarCadastrar = new Logar;

//Função para logar
if(isset($_POST['logar'])):
   $email = trim(strip_tags($_POST['email']));
   $senha = trim(strip_tags($_POST['senha']));
   $verificar = $logarCadastrar->Consulta("SELECT * FROM CS_usuarios WHERE email = ? AND senha = ?","ss","{$email}","{$senha}");
   if($verificar >= 1):
       // return "Encontrado";
       print_r(json_encode("Encontrado"));
   else:
       // return "Não encontrado";
       print_r(json_encode("Não encontrado"));
   endif;
  endif;
    
13.04.2018 / 22:18
1

Your ajax is expecting a return of type JSON, in PHP do the following:

echo json_encode("Aqui o que deseja retornar");
    
18.10.2016 / 15:06