Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.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 字符串匹配在js中不起作用?_Javascript_Html_Ajax - Fatal编程技术网

Javascript 字符串匹配在js中不起作用?

Javascript 字符串匹配在js中不起作用?,javascript,html,ajax,Javascript,Html,Ajax,当我试图用字符串检查Ajax响应时,总是打印失败 这是我的ajax部分 var username = document.getElementById("name").value; var password = document.getElementById("password").value; var dataString = 'email='+ username + '&password='+ password; $.ajax({ url:"http://test.com

当我试图用字符串检查Ajax响应时,总是打印失败

这是我的ajax部分

var username =  document.getElementById("name").value;
var password =  document.getElementById("password").value;
var dataString = 'email='+ username + '&password='+ password;


$.ajax({
   url:"http://test.com/login.php",
  type: "post",
  data: dataString,
  success: function(data) {
  alert(data);
      var test = data.replace(/(\r\n|\n|\r)/gm," ");
      alert(test); //working
      if(test == "1"){   //condition failed
          alert("h");
      }else{
          alert("fail");  //printing this
      }
  },
  error: function(xhr, desc, err) {
    console.log(xhr);
    console.log("Details: " + desc + "\nError:" + err);
  }
});

alert

我猜test是一个类似“1”的字符串,里面有空格

data.replace(/(\r\n |\n |\r)/gm,“”;

//而不是数据。替换(/(\r\n |\n |\r)/gm,“”
我猜
测试不等于
“1”
。您的代码/注释似乎证实了这一点。它是1..我可以在alertit中看到它可能是
1
,但它不是
“1”
。请注意,
1
可能有一些隐藏字符未显示在警报中。您是否考虑过使用控制台而不是警报对此进行调试?控制台可以在网络选项卡中显示响应的原始视图。好的,让我试试