If within a success in ajax

0

I make the request, I get the right value, but at the time of verification it does not work

                $.ajax({
                    url:'includes/checkCPF.inc.php',
                    method:'POST',
                    type:'POST',        
                    data:{cpf:cpf},
                    success:function(data){
                        alert(data);
                    if(data == "V")
                    {
                        alert("b");   
                        $(".cpf").addClass("ShadowRed");
                        $(".cpf").focus();
                        return
                    }
                 });

php:

<?php
include_once '../acesso.php';
$cpf = $_POST["cpf"];
$sql = "SELECT * FROM login_usuario WHERE CpfUsuario = '$cpf'";
$pedido = $conn->query($sql);
if(mysqli_num_rows($pedido) != 0)
{
    echo "V";
}
else
{
    echo "F";
}
    
asked by anonymous 22.02.2018 / 20:09

1 answer

3

Returning Ajax with echo to receive a string can return whitespace at the ends of the string, and this will make a difference in if that will differentiate the V from a V with spaces before and / or later.

Clean these spaces returned in data with method .trim() :

if(data.trim() == "V")

Also missing success: with keys } :

$.ajax({
   url:'includes/checkCPF.inc.php',
   method:'POST',
   type:'POST',        
   data:{cpf:cpf},
   success:function(data){
      alert(data);
      if(data.trim() == "V")
      {
         alert("b");   
         $(".cpf").addClass("ShadowRed");
         $(".cpf").focus();
         return;
      }
   } ← FECHAR AQUI
});
    
22.02.2018 / 20:14