输入“0”时,jQuery .ajax调用失败

时间:2010-09-21 06:54:17

标签: jquery ajax

我已经使用jQuery和PHP创建了一个猜数字游戏,虽然它在输入数字时工作正常(并且也很好地处理非数字),但是当输入0时,Ajax调用失败。据我所知,当输入0时,发送到服务器端代码的变量$guess设置不正确,但我无法理解为什么 - 任何想法?

表格输入:

<form id="guess" action="magicgubbins.php" method="get">
    <p><input type="text" id="newguess" name="guess" value="" />
    <input type="submit" value="Guess"></p>
</form>

jQuery的:

<script type="text/javascript">
$(function() {
    var magicno = Math.floor(Math.random()*11);

    $('#guess').submit(function() {

        var newguess = $('#newguess').attr('value');
        if (!newguess) {
            newguess = 'nonumber';
        }

        var form = $(this),
        //formData = form.serialize(),
        formData = {guess: newguess, magic: magicno},
        formUrl = form.attr('action'),
        formMethod = form.attr('method'),
        responseMsg = $('#magicbox');

        //show waiting message
        responseMsg.hide()
            .addClass('response-waiting')
            .text('Please wait...')
            .fadeIn(200);

            $.ajax({
                url: formUrl,
                type: formMethod,
                data: formData,
                success: function(data){
                    // parse returned data
                    var responseData = jQuery.parseJSON(data),
                        klass = '';

                    switch(responseData.status){
                        case 'error':
                            klass = 'response-error';
                        break;
                        case 'success':
                            klass = 'response-success';
                        break;
                    }

                    // show new response message

                    responseMsg.fadeOut(200,function(){
                        $(this).removeClass('response-waiting')
                            .addClass(klass)
                            .text(responseData.message)
                            .fadeIn(200,function(){
                                if (responseMsg.hasClass('response-success')) {
                                    $('#description').hide();
                                    $('#magicbox').hide();
                                    $('#status').text('You win!').show();
                                }

                                // timeout response message
                                setTimeout(function(){
                                    responseMsg.fadeOut(200,function(){
                                        $(this).removeClass(klass);
                                    });

                                },3000);
                            });
                    });

                }
            });



    return false;
    });
});

</script>

服务器端代码:

<?php


$magic = htmlentities($_GET['magic']);
$guess = htmlentities($_GET['guess']);


if(empty($magic)){ 
    $status = "error";
    $message = "No magic number set!";
} else if (empty($guess)) {
    $status = "error";
    $message = "Take a guess!";
} else {
    if(is_numeric($guess)) {
        if($guess == $magic) {
            $status = "success";
            $message = "Correct! " . $magic . " is the magic number.";
        } else {
            if ($guess > $magic) {
                $status = "error";
                $message = "Wrong! " . $guess . " is too high.";
            } else {
                $status = "error";
                $message = "Wrong! " . $guess . " is too low.";
            }
        }
    } else {
            $status = "error";
            $message = "That's not a number!";
    }
    $data = array(
    'status' => $status,
    'message' => $message
    );
    echo json_encode($data);

exit;

}
?>

1 个答案:

答案 0 :(得分:1)

您是否使用Firebug Net控制台窗口(或Google Chrome中的等效窗口)调试了呼叫?

另外请不要忘记PHP将数字0视为false,将布尔运算符中的空字符串视为false。调用空(0);将返回真实。调用空(“0”);不会。