-1

I encounter this problem when I use AJAX in javascript.

function showUsernameStatus() {
 if (usernameRequest.readyState == 4) {
  if (usernameRequest.status == 200) {
   console.log(typeof(usernameRequest.responseText));
   console.log(usernameRequest.responseText);
   if (usernameRequest.responseText == 'ok') {
    console.log(111);
    document.getElementById('username').className = 'approved';
    usernameValid = true;
   }else{
    console.log(222);
    document.getElementById('username').className = 'denied';
    document.getElementById('username').focus();
    document.getElementById('username').select();
    usernameValid = false;
   }
  }
  checkFormStatus();
 }
}
And in the chrome console plate it displays: string ok 222 in order;

The value of usernameRequest.responseText is ok and the type is string, however it dose not equal to the string 'ok'. please tell me why, thank you very much!!!

sydridgm
  • 1,012
  • 4
  • 16
  • 30
  • 1
    `console.log(escape(usernameRequest.responseText));` will show you why. If the string is not equal than it is not equal. You have other characters in there. – epascarello Jul 20 '15 at 02:27
  • Why you did not add this code to your previous post, but ask a new question? – fuyushimoya Jul 20 '15 at 02:27
  • @AVD how do you know that? (asking because I'm curious how it's possible to get it from this code) (not to say that you **can** compare a string object and a string literal) – zerkms Jul 20 '15 at 02:30
  • @AVD What in the world are you talking about. lol A string is a string, – epascarello Jul 20 '15 at 02:31
  • @epascarello I'm sure they meant a string object created with `String` constructor. Eg: `var so = new String('foo');` (that wouldn't be a problem anyway though) So "a string is a string" is not 100% accurate. – zerkms Jul 20 '15 at 02:34
  • @AVD to be fair - have you tried (or do you understand) to check how `==` operator works with a string literal and a string object operands? A hint: it works fine. – zerkms Jul 20 '15 at 02:36
  • @AVD Dude, that is not the issue here.... The issue is going to be the server is returning other characters in the string like the 100s of exact questions that are just like this and is why JSON is always a better solution than returning a string. – epascarello Jul 20 '15 at 02:37
  • @ epascarello yeah~~ it displays '%uFEFFok'. But in my php file:[ echo 'ok';] it dose not contain any other character. – sydridgm Jul 20 '15 at 02:42
  • that's why you should use JS libraries like jQuery instead of writing your own AJAX scripts. And for the Unicode character (yeah, that's the famous BOM), read [this](http://stackoverflow.com/questions/18029992/how-do-i-remove-a-ufeff-character-in-my-webpage-scripts) – Raptor Jul 20 '15 at 03:16
  • 1
    @Raptor yeah!! I have solved it. I used jQuery several times and now I want to try something new~~~ thank you a lot. – sydridgm Jul 20 '15 at 04:11
  • Try: usernameRequest.responseText.trim(). You may have some leading or trailing spaces – Gregg Jul 20 '15 at 05:25
  • @Gregg yes. you are right~ – sydridgm Jul 20 '15 at 06:12

1 Answers1

0

According to the comment of OP, the response text is actually %uFEFFok, which contains a BOM (byte order mark) character at the beginning of response.

In order to remove the BOM, either trimming the text in JS side or removing the BOM in PHP file.

Reference: Byte Order Mark

Raptor
  • 53,206
  • 45
  • 230
  • 366