Bangalore Bangalore - 6 months ago 20
HTML Question

string matching not working in js?

when i tried to check my Ajax response with string then its always printing fail case.

Here is my ajax part

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 enter image description here

Answer

what I am guessing is that test is a string like "1 ", there are whitespace inside

data.replace(/(\r\n|\n|\r)/gm,""); 
// instead of data.replace(/(\r\n|\n|\r)/gm," ")

Comments