javascript - jQuery 基本 ajax GET + php 返回

标签 javascript php jquery ajax

编辑:修复了 _ 拼写错误 (2x),添加了标题,仍然记录 100。

在我的 JavaScript 中单击一个按钮后,我将触发此函数(参数:100)

ajaxManager = new AjaxManager();
ajaxManager.requestHexContent(100);

function AjaxManager (){
    this.requestHexContent = function(id){
        $.ajax({
            type : 'get',
            url : 'simulator/hexFiller.php',
            dataType : 'json',
            data: {
                gameid: id,
            },
            success : function(ret){
                console.log(ret);
            },
            error : function(){
                alert("error")
            }
        });
    },
}

这是我的hexFiller.php

<?php   
header('Content-Type: application/json');

  $ret; 

  if (isset($_GET["gameid"])){
    if ($_GET["gameid"] == 100){
        $ret = 200;
    }
   else {
    $ret = "error";
 }
}

echo json_encode($ret);

?>

现在,我希望我的浏览器将“200”或“错误”记录到控制台。 相反,它将“100”记录到控制台。

有人可以向我解释一下我的想法中的根本错误吗?

最佳答案

正如评论中所讨论的,下面提到了工作代码: 我只用 $_GET 替换了 $GET。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    function AjaxManager() {
        this.requestHexContent = function (id) {
            $.ajax({
                type: 'get',
                url: 'simulator/hexFiller.php',
                dataType: 'json',
                data: {
                    gameid: id,
                },
                success: function (ret) {
                    console.log(ret);
                },
                error: function () {
                    alert("error")
                }
            });
        }
    }

    ajaxManager = new AjaxManager();
    ajaxManager.requestHexContent(100);
</script>

hexFiller.php

<?php
$ret;

if (isset($_GET["gameid"])) {
    if ($_GET["gameid"] == 100) {
        $ret = 200;
    } else {
        $ret = "error";
    }
}

echo json_encode($ret);
?>

关于javascript - jQuery 基本 ajax GET + php 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33154246/

相关文章:

javascript - Google 放置 : Use multiple types

javascript - 隐藏背景直到值改变

php - 从 PHP 页面上的 jQuery AJAX 调用获取 JSON 数据

jquery - 如何在 Django 中压缩 JSON 请求?

jquery - 获取 puppeteer 中元素的子元素

javascript - JavaScript 中 instanceOf 函数的实现

php - jQuery keydown 功能不起作用

php - 在 $_POST 中找不到输入类型=图像值

php - 取消授权页面选项卡通知

javascript - 是否可以像 jQuery 那样通过 id 选择元素?