Php AJax忽略响应
我在我的网站上运行一些ajax来处理表单提交,然而,出于某种原因,每当我检查PHP的响应时,它都会忽略我的处理并直接进入底部Php AJax忽略响应,php,jquery,ajax,if-statement,comparison,Php,Jquery,Ajax,If Statement,Comparison,我在我的网站上运行一些ajax来处理表单提交,然而,出于某种原因,每当我检查PHP的响应时,它都会忽略我的处理并直接进入底部 //Login Form Submission $('form.loginform').on('submit', function (e) { e.preventDefault();0 //Grab Data var that = $(this), url = that.attr('action'), method = that.attr('method'
//Login Form Submission
$('form.loginform').on('submit', function (e) {
e.preventDefault();0
//Grab Data
var that = $(this),
url = that.attr('action'),
method = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: method,
data: data,
success: function (response) {
console.log("Ajax Response: \"" + response + "\"");
console.log(typeof response);
if(response === "0"){
document.getElementById('error_message').innerHTML = "Incorrect password";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
}else if(response === "1"){
window.location.href = "./index.php";
}else if(response === "2"){
document.getElementById('error_message').innerHTML = "Please fill all required fields.";
document.getElementById('error_message').style.color = 'red';
} else if(response === "3"){
document.getElementById('error_message').innerHTML = "That username does not exist. Please try again.";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
} else if(response === "4"){
document.getElementById('error_message').innerHTML = "Your account has been banned on this website. Please contact an administrator.";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
} else {
document.getElementById('error_message').innerHTML = "Error while logging in. Please try again.<br />If you continue to recieve this error, Please contact an administrator.";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
}
//This is the output from the php script
}
});
return false;
});
然而,它随后直接转到
'Error while logging in, Please contact an Administrator'
我的问题是,为什么它会忽略“else”之上的“if”语句,即使响应是“1”?试试看
response.trim()=“1”
由于日志显示其1和字符串,因此可能会出现一些空白。请尝试
response.trim()=“1”
由于日志显示其1和字符串,因此可能会出现一些空白。请尝试
response.trim()=“1”
由于日志显示其1和字符串,因此可能会出现一些空白。请尝试
response.trim()=“1”
因为日志显示其1和字符串,所以可能存在一些空白。您可以尝试响应吗。trim()=“1”因为日志显示其1和字符串,所以可能存在一些空白?就是这样!非常感谢你!(我假设我在执行“console.log”时在字符串周围加了引号,所以没有空格,但就是这样。非常感谢!(如果你希望我接受,你可以重新发布答案)好的,我已经发布了答案!!你能尝试response.trim()=“1”吗因为日志显示了它的1和字符串,所以可能会有一些空白?就是这样!非常感谢!(我假设在执行“console.log”时在字符串周围加上了引号,所以没有空白,但就是这样。非常感谢!(如果你想让我接受,可以重新发布作为答案)好的,我已经发布了答案!!你能试试response.trim()=“1”吗,因为log显示了它的1和字符串,所以可能有一些空白?就是这样!非常感谢!(我假设在执行“console.log”时在字符串周围加了引号,所以没有空白,但就是这样。非常感谢!(如果你想让我接受的话,你可以将其重新发布为答案)好的,我已经发布了答案!!你可以尝试一下response.trim()=“1”吗,因为log显示了它的1和字符串,所以可能会有一些空白?就是这样!非常感谢!(我假设,因为我在执行“console.log”时在字符串周围加了引号)没有空白,但就是这样。非常感谢!(如果你想让我接受,你可以重新发布答案)好的,我已经发布了答案!!
'Error while logging in, Please contact an Administrator'