javascript 运行 ajax 需要什么 - 将 jquery 添加到页面处理 ajax 调用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29024586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:58:58  来源:igfitidea点击:

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

javascriptphpjqueryajax

提问by Abid Ali

I am new to ajax and running xampp on my localhost I want to communicate with server by using ajax in my own created page.

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

I want to know is there anything required to add in head section to use ajax calls ?

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

In my web page's head section there is no javascript file incluided other than this jquery file

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

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

is this sufficient to make ajax calls like

这是否足以进行 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>

and in functions.phpfile

并在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();
}
?>

but there is no response, I tried several changing but no success. Is there a fault in my code or I am not including the ajax connection file or something ? Any help would be much appreciated.

但是没有任何反应,我尝试了几次更改但都没有成功。我的代码是否有问题,或者我没有包含 ajax 连接文件或其他内容?任何帮助将非常感激。

采纳答案by itssajan

From your code here

从你的代码在这里

<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>

you have commented the data that is being sent to the functions.phppage. And in functions.phppage you are checking for $_GET['id']which is not present.

您已经评论了发送到functions.php页面的数据。在functions.php页面中,您正在检查$_GET['id']哪个不存在。

Other than that there is no problem in your code, considering you have the right db connection and the jQuery file is loaded.

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



If you still have doubt then go with the following code for just testing

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

<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

functions.php

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