代码之家  ›  专栏  ›  技术社区  ›  whostolemyhat

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

  •  0
  • whostolemyhat  · 技术社区  · 14 年前

    我已经使用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 回复  |  直到 14 年前
        1
  •  1
  •   Sam Day    14 年前

    您是否使用Firebug Net控制台窗口(或googlechrome中的等效窗口)调试过这个调用?

    另外,别忘了PHP在布尔运算符中将数字0视为false和空字符串。调用empty(0);将返回true。调用empty(“0”);将不起作用。