1

I'm not able to do an accurate string compare with the results of my AJAX script. For some reason, my if statement always fails. By removing the if test, I can see that the correct value ("success") is being returned, but when I leave it in, it evaluates to false. What am I doing wrong? Thanks.

HTML File:

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>

The part of the php script that is relevant is just the last line:

echo "success";
4

1 回答 1

2

试一试这个功能,让我们看看会发出什么警报

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;
}
于 2013-07-02T20:48:11.230 回答