尝试发送数据时出现AJAX/JQuery错误


AJAX/JQuery error when trying to send data

不知道为什么,但我添加了一个"error"子句来确保AJAX失败。。。确实如此。它一直工作到AJAX部分,只是不发送数据,不知道为什么。

<script type="text/javascript">
<!--
$(document).ready(function() {
$("#login").click(function() {
    document.getElementById("result").innerHTML = 'Validating credentials...';
    var un = $("#un").val();
    var pw = $("#pw").val();
    if ( un == "" )
    {
        document.getElementById("un_error").style.visibility = 'visible';
        $("#un").focus();
    }
    if ( pw == "" )
    {
        document.getElementById("pw_error").style.visibility = 'visible';
        $("#pw").focus();
    }
    $.ajax({
        type: 'POST',
        url: 'login-parse.php',
        data: { un: un, pw: md5(pw) },
        success: function(msg) {
                document.getElementById("result").innerHTML = msg;
                    },
        error: function(xhr, status) { alert(status); }
    });
});
});
//-->
</script>

这就是JS代码。

这是HTML:

    <div id="content">
    <div id="result" class="result"></div>
    <h2>Login To Your Account</h2>
    <div class="text">
        <fieldset>
            <fieldset>
                <legend>Username</legend>
                <input type="text" id="un" value="" size="20" /><span class="error" id="un_error">*</span>
            </fieldset>
            <fieldset>
                <legend>Password</legend>
                <input type="password" id="pw" value="" size="30" /> <span class="error" id="pw_error">*</span>
            </fieldset>
            <input type="button" id="login" value="Login" />
        </fieldset>
    </div>
</div>
<?php
// Login Parser
require 'inc.common.php';
if (! isset ( $_POST['un'], $_POST['pw']) )
{
    echo '<blockquote>Invalid username/password combination.</blockquote>' . "'n";
} else {
    $un = $_POST['un'];
    $pw = md5($_POST['pw']);
    $check = $sql->result ( $sql->query ( 'SELECT COUNT(*) FROM `users` WHERE `user_name` = ''' . $sql->escape($un) . ''' AND `user_password` = ''' . $sql->escape($pw) . '''' ) );
    $errors = array();
    if (! strlen ( $un ) )
        $errors[] = 'Please enter a valid username.';
    if (! strlen ( $pw ) )
        $errors[] = 'Please enter a valid password.';
    if ( $check == 0 )
        $errors[] = 'Invalid username/password combination.';
    if ( count ( $errors ) > 0 )
    {
        echo '<blockquote>' . "'n",
             '  The following errors occurred with your login:' . "'n",
             '  <ul>' . "'n";
        foreach ( $errors as $enum => $error )
        {
            echo '      <li><strong>(#' . ($enum+1) . '):</strong> ' . $error . '</li>' . "'n";
        }
        echo '  </ul>' . "'n",
             '</blockquote>' . "'n";
    } else {
        setcookie ( 'ajax_un', $un, time()+60*3600 );
        setcookie ( 'ajax_pw', $pw, time()+60*3600 );
        echo '<blockquote>' . "'n",
             '  <p><strong>Success!</strong></p>' . "'n",
             '  <p>You have successfully been logged in as <strong>' . $un . '</strong>.</p>' . "'n",
             '  <p>You may now return to the <a href="index.php">index page</a>.</p>' . "'n",
             '</blockquote>' . "'n";
    }
}
?>

从Firebug控制台中的消息"500-内部服务器错误"和您发布的代码来看,数据库操作似乎存在一些问题。

因此,请尝试将查询存储为变量。回显它,并尝试在phpMyadmin或其他用于访问数据库的等效客户端中执行它。

如果可能的话,也发布结果。