Performs the operation but displays error at the end

0

I have the code below that is taking me seriously.

No people are possible!

// 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 exactly same files with the exception of only the variable names.

I have a alert that is printing OK here

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

But do not enter the correct block.

And of course it does not reload .

Where am I going wrong?

Detail, deletion is normally done.

    
asked by anonymous 26.05.2018 / 00:17

1 answer

1

The Ajax return comes with white space at the edges and not just the pure string.

To resolve this, clear the return with the .trim() method, so make sure 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")){
   ...
}
    
26.05.2018 / 00:33