compare a chain of javascript

3

I have an ajax script which goes and queries a mysql DB in a php, if there is a record I return an echo "success"; and if not "without success";

$total = mysql_num_rows(mysql_query("SELECT * FROM dispositivos WHERE serie ='$serial'"));
if($total==0){
    echo "sin exito";
}else
{
  $sql=mysql_query("DELETE FROM dispositivos WHERE serie ='$serial'");
  echo "exito"; 
}

this success or without success I keep it in

var respuesta = ajax.responseText

if I make an alert (answer);

Effectively shows me success or no success depending on the result of PHP, so I know that until here everything is fine

What I need is to do more things if it is a success, for which I did a

if(respuesta=="exito")
    {
      // cosas que voy a hacer
    }

here's the problem, it does not enter the if, despite being "success" it passes it by, I tried with = and with ==

I do not know if I can not compare a variable with a string but it is assumed that the variable has a string so it could.

    
asked by Gabriel Uribe Gomez 19.09.2017 в 20:57
source

2 answers

0

To be sure I would use the '===' operator to do a string comparison in a strict manner like this:

if(respuesta === "exito") {
   // cosas que voy a hacer
}

The == operator is less strict and in fact is comparing two objects, it would work if you do something like this:

if(new String(respuesta).valueOf() == new String("exito").valueOf()) {
   // cosas que voy a hacer
}

Successes!

    
answered by 19.09.2017 в 22:33
-2

You can use trim is to remove the blanks:

if(respuesta.trim() === "exito") {
   // cosas que voy a hacer
}
    
answered by 07.11.2018 в 01:47