Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 与字符串进行比较时,变量看起来为空_Javascript - Fatal编程技术网

Javascript 与字符串进行比较时,变量看起来为空

Javascript 与字符串进行比较时,变量看起来为空,javascript,Javascript,信息被发送到我的另一个页面,在那里被处理并返回结果。我可以用console和alert读取它,但当我将它们与字符串进行比较时,它不起作用。我还测试了typeof(),它还以字符串形式返回类型 有时由于额外的空白会出现此问题,请尝试以下操作: //this is my hmlhttprequest var ajax = new XMLHttpRequest(); ajax.open("GET","mydomain/reg-process.php?info="+userVal+"&am

信息被发送到我的另一个页面,在那里被处理并返回结果。我可以用console和alert读取它,但当我将它们与字符串进行比较时,它不起作用。我还测试了
typeof()
,它还以字符串形式返回类型

有时由于额外的空白会出现此问题,请尝试以下操作:

 //this is my hmlhttprequest
 var ajax = new XMLHttpRequest();
     ajax.open("GET","mydomain/reg-process.php?info="+userVal+"&input=username",true);
     ajax.send();
     ajax.onreadystatechange = function(){
      if(ajax.readyState == 4 && ajax.status == 200){
       var gotuser = ajax.responseText;
       console.log('results=>'+gotuser);// it alert/console.log 'userok'.
       if(gotuser ==="userok"){  // its like the gotuser is empty,if i compare 'userok' against 'userok' it works."
          do something; // when variable matchs the string.
       }
     }
     }

很有效,非常感谢你。其他问题。如何编写代码并使其可读,保留空格和注释而不产生所有这些麻烦?gotuser.trim().toLowerCase()=“userok”。只是为了让它更简单。@DavidAndrade如果对您有效,请将该解决方案标记为已接受。因为它也将对其他人有很大帮助。:-)我该怎么做?@DavidAndrade只需点击答案左侧投票信息下方的勾号。
if(gotuser.trim() ==="userok"){  
          do something; 
}