运行 AJAX 需要什么 - 将 jQuery 添加到页面处理 AJAX 调用

what is required to run ajax - does adding jquery to page processes ajax calls?

本文关键字:AJAX 处理 调用 添加 什么 运行 jQuery      更新时间:2023-09-26

我是 ajax 的新手,在我的本地主机上运行 xampp我想通过在我自己创建的页面中使用 ajax 与服务器通信。

我想知道是否需要在头部部分添加任何内容才能使用 ajax 调用?

在我的网页的头部部分,除了这个jquery文件之外,没有包含javascript文件

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

这是否足以使 Ajax 调用像

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            //data: {id: 'enter_game'},
            dataType: 'json',
            cache: false,
            success: function(result) {
            if(result){
                resultObj = eval (result);
                alert( resultObj );
            }else{
                alert("error");
            }
            }
        });
        });
</script>

并在functions.php文件中

<?php
include("connection_to_db.php");
function is_sufficient_users()
    {
        $result = mysql_query("SELECT * FROM ".$dbprefix."users") or die ( mysql_error() );
        if(mysql_num_rows($result) >= 3 ){
            // sufficient users
            // start the game
            $data = array();
            //$word = start_game();
            $word = 'Apple';
            while($row = mysql_fetch_assoc($result)){
                $data['id']['name'] = $row['name'];
                $data['id']['ghost'] = $row['ghost']; 
            }
            $data['word'] = $word;
            $data['game'] = "Game Starts";
            echo json_encode($data);
        }else{
            $data = array();
            switch(mysql_num_rows($result))
            {
                case 0:
                $waiting = 3;
                break;
                case 1:
                $waiting = 2;
                break;
                case 2:
                $waiting = 1;
                break;
                default:
                $waiting = 3;
                break;
            }
            $data['game'] = "Waiting for ".$waiting." more users to start the game" ;
            echo json_encode($data);
        }
    }
if(isset($_GET['id']) && strtolower($_GET['id']) == "enter_game"){
    is_sufficient_users();
}
?>

但是没有回应,我尝试了几次更改但没有成功。我的代码中是否有错误,或者我没有包含ajax连接文件或其他内容?任何帮助将不胜感激。

从你的代码这里

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            //data: {id: 'enter_game'},
            dataType: 'json',
            cache: false,
            success: function(result) {
            if(result){
                resultObj = eval (result);
                alert( resultObj );
            }else{
                alert("error");
            }
            }
        });
        });
</script>

您已注释要发送到functions.php页面的数据。在functions.php页面中,您正在检查不存在的$_GET['id']

除此之外,考虑到您有正确的数据库连接并且加载了 jQuery 文件,您的代码中没有问题。


如果您仍然有疑问,请使用以下代码进行测试

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            cache: false,
            success: function(result) {
               console.log(result);
            }
        });
    });
</script>

functions.php

<?php
echo "This is just to see if Ajax is working and it does !";