我无法与我的AJAX脚本的结果进行准确的字符串比较。出于某种原因,我的if语句总是失败。通过删除if测试,我可以看到正在返回正确的值(“success”),但是当我将其保留时,它的计算结果为false。我究竟做错了什么?感谢。
HTML文件:
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Sign In</title>
<link rel="stylesheet" type="text/css" href="styles/mystyles.css" media="screen" />
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
window.onload = function ajaxFunction() {
document.loginform.onsubmit = attemptLogin;
}
function attemptLogin() {
var username = document.getElementById("name").value;
var password = document.getElementById("password").value;
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = $.trim(xmlhttp.responseText).toLowerCase();
//var response = xmlhttp.responseText.trim();
//var response = xmlhttp.responseText;
if (response == "success") {
// window.location.href = "dashboard.html";
//} else {
document.getElementById("feedback").innerHTML=response;
//}
}
}
xmlhttp.open("GET",
"scripts/login.php?username="+username+"&password="+password
,true);
xmlhttp.send();
return false;
}
//-->
</script>
<div id="frame">
<div id="page">
<img id="mainpic" src="images/banner.png">
<div id="leftsidebox"></div>
<div id="stylized" class="myform">
<form id="form" name="loginform">
<h1>Mobile App Data Administration</h1>
<p>Sign in to update, modify, or delete data used by the mobile app.</p>
<label>Username
<span class="small">Enter your username</span>
</label>
<input type="text" name="username" id="name" autofocus="autofocus" />
<label>Password
<span class="small">Enter your password</span>
</label>
<input type="password" name="pass" id="password" />
<div id="feedback"></div>
<button type="submit">Log In</button>
</form>
<button type="submit" class="lowerbutton" onClick="parent.location='newuser.html'">New User</button>
</div>
</div>
</div>
</body>
</html>
相关的php脚本部分只是最后一行:
echo "success";
答案 0 :(得分:1)
给这个功能一个机会,让我们看看会得到什么警告
function attemptLogin()
{
var username = document.getElementById("name").value;
var password = document.getElementById("password").value;
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var response = xmlhttp.responseText;
if (response == "success")
{
alert("log you in");
window.location.href = "dashboard.html";
}
else
{
alert("not equal to success");
document.getElementById("feedback").innerHTML = response;
}
}
}
xmlhttp.open("GET", "scripts/login.php?username=" + username + "&password=" + password, true);
xmlhttp.send();
return false;
}