Return getJSON

Asked

Viewed 67 times

0

When getJSON, does it not find what is returned? I’m leaving as Fazio, but not the right one.

$("#email").focusout(function() {
            emailDigitado = $("#email").val();
            $.getJSON("inc_verificaCadastro.php", {email:emailDigitado}, function(json){

                if (json[0].email == ''){
                    $('#divSenha').attr('class', 'mostrar');
                    $("#senha").attr("required", true);
                }else{
                    $("#id_cadastro").val(json[0].id_cadastro);
                }
            });
        });

inc_verificationCadastro.php file

<?php
header('Content-Type: application/json; charset=utf-8');
include 'config.php';

$rs = $conexao->query("SELECT ID_Cadastro, email FROM cadastro WHERE email = '".$_GET['email']."' ");

$Array = Array();
while($row = $rs->fetch_assoc()) {
    $Array[] = Array(
        "email"         => $row['email'],
        "id_cadastro"   => $row['ID_Cadastro']
    );
}
$json_encode = json_encode($Array);
echo $json_encode;
?>
  • @bfavaretto I edited the initial post. Still not entering the initial IF.

  • @bfavaretto Ahhhh... see if I’m right. When the search is not done successfully json[0].email returns Fazio. So you have no way to compare anything with empty.

  • 2

    Test whether json.length > 0 before tampering with the server response

  • @bfavaretto It worked, I put json.length != 1

  • ??? != 1 it will enter if you have 0 or 2+, and Else if you have exactly 1.

  • @bfavaretto Here it worked so....

Show 1 more comment

1 answer

0

To check if the json is empty, try the following

if (!$.trim(json)){   
    alert("a variavel json esta em branco " + json);
}
else{   
    alert("a variavel json não esta em branco: " + json);
}

Browser other questions tagged

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