Ajax does not recognize the echo of PHP [closed]

1

I have a simple ajax:

$.ajax({
    url:"php/exemplo.php",
    type:"POST", 
    data:new FormData(this),
    contentType:false,
    cache:false,
    processData:false,
    success: function(resposta){
      if(resposta == '<nome>'){
        alert('deu certo');
      }
}});

In PHP I give echo "<nome>"; and this <nome> arrives in ajax right (I checked with an alert) ... but if it does not understand that it is <nome> , it does not fall into if ...

What could be the error?

    
asked by anonymous 29.09.2017 / 22:24

4 answers

1

The error was what they were talking about, some empty PHP space was coming. I was able to adjust using Jquery trim: jQuery.trim ()

Looking like this:

if($.trim(resposta) == '<nome>'){
    
02.10.2017 / 15:35
3

Probably the Ajax return is coming with some space or line break along with <nome> , so if understands that answer is not exactly <nome> .

One way to work around this is by changing the comparison way with indexOf :

if(resposta.indexOf('<nome>') != -1){
  alert('deu certo');
}
    
29.09.2017 / 22:30
1

You can use the native .trim() of JavaScript and make if(resposta.trim() == '<nome>'){ .

So clean up blanks and line breaks before the comparison.

    
02.10.2017 / 15:36
-1

Make sure that the <nome> obtained in AJAX does not have any more space before or after s and you are using UTF-8 without BOM.

    
30.09.2017 / 19:39