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

时间:2013-07-05 20:45:27

标签: php javascript html ajax

不确定原因,但我添加了一个“错误”子句以确保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";

    }

}

?>

1 个答案:

答案 0 :(得分:0)

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

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

如果可能,也发布结果。