Performs the operation but displays error at the end

Asked

Viewed 24 times

0

I’ve got the code below that’s really pissing me off.

It’s not possible people!

// JavaScript Document// JavaScript Document
$(document).ready(function(e) {

  $("a.excluiFalecimento").click(function() {

      if (confirm('Deseja Excluir esta Nota?') ) {

           $.post ("../_requeridos/excluiFalecimento.php", {

               idFalecimento   : $(this).attr('falecimentoid')

           }, function(retorno){
               alert(retorno)
               if (retorno == "OK") {

                    alert('Excluido com sucesso');
                    location.reload();

                } else {

                    alert("Erro na exclusão");

                }

             }
        );

        return false;

      }

  })


});

I have 4 files exactly the same except for the variable names only.

I got a alert who is printing OK

               alert(retorno)
               if (retorno == "OK") {

But it doesn’t enter the correct block.

And it obviously doesn’t do the Reload.

Where am I going wrong?

Detail, deletion is being done normally.

1 answer

1


The return of Ajax comes with white spaces on the edges and not just the pure string.

To solve this, clear the return with the method .trim(), so it is assured to return only the string OK and the if will validate:

if (retorno.trim() == "OK") {
   ...
}

Or (less recommended) you can check if the return has the string OK:

if(~retorno.indexOf("OK")){
   ...
}
  • yes, indeed that was it. But why did the other 3 identical files not give this problem? Note: I have already looked at his GOOD signature. There is not. It’s like UTF without GOOD

  • rs.. I cannot say, each case is to be analyzed.

  • Due to the doubts, in these cases where a string is expected and you want to make an extended comparison on if, it is good to use .trim(). Or else you could use if(~retorno.indexOf("OK")){...}

Browser other questions tagged

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