【问题标题】:Get data in Ajax doesnt work在 Ajax 中获取数据不起作用
【发布时间】:2011-10-21 19:21:48
【问题描述】:

我有这个 Jquery Javascript 代码:

$(document).ready(function(){
    $("#check_button").click(function(){
        $.ajax({
            url: "includes/adcontent.php",
            type: "GET",
            data: "id=<?php echo $id; ?>",
            async: false,
            cache: false,
            beforeSend: function (data) {
                    $('#check_quest_button_div').html("<img src='/images/icons/loading.gif' title='Please wait' alt='Please wait' />");
            }
        });
    });
});

这是我在 include/adcontent.php 中的 PHP 代码:

if (!isset($id) && isset($_GET['id'])){
    $id=htmlspecialchars(strip_tags($_GET['id']));
}
echo $id;

但是使用此代码时,我会收到一条错误消息。该脚本不发送 GET 数据。有什么问题?

【问题讨论】:

  • 不要作为数据传递,作为查询参数传递(在includes/adcontent.php后添加?id=&lt;?php echo $id; ?&gt;
  • 您能对其进行wireshark 并确保正在发出请求吗?
  • 是的,请求正常。如果我放置代码 echo "ok";,我可以看到它。
  • 这是我的错误:未定义的变量:id 我还可以看到 GET 是空的:[_GET] => Array ()

标签: jquery ajax get send


【解决方案1】:

你试过了吗:

$.ajax({
    url: "includes/adcontent.php?id=<?php echo $id; ?>",
    type: "GET",
    async: false,
    cache: false,
    beforeSend: function (data) {
        $('#check_quest_button_div').html("<img src='/images/icons/loading.gif' title='Please wait' alt='Please wait' />");
    },
    success: function (data) {
        alert(data);
    }
});

你看到警报了吗?

【讨论】:

  • 是的,我看到了带有正确变量的警报。很奇怪,因为如果我用我的代码替换你的“成功代码”,它就不起作用了。
  • 那么该代码可能有错误。我还更改了您将数据发送到服务器的方式,因此也可以这样做。我将它们作为查询字符串发送,您将它们作为正文数据发送,根据规范,GET 请求中不允许这样做。
  • 好吧,我有办法了。我还应该使用 ?id=。这都是我的错。抱歉,非常感谢您的帮助!!!
【解决方案2】:

试试这个

$(document).ready(function(){
    $("#check_button").click(function(){
        $.ajax({
            url: "includes/adcontent.php?id=<?php echo $id; ?>",
            type: "GET",
            async: false,
            cache: false,
            beforeSend: function (data) {
                    $('#check_quest_button_div').html("<img src='/images/icons/loading.gif' title='Please wait' alt='Please wait' />");
            }
        });
    });
});

【讨论】:

  • @Jordy - 你至少看到请求通过了吗?
  • 是的,请求正常。如果我放置代码 echo "ok";,我可以看到它。
  • 这是我的错误:未定义的变量:id 我还可以看到 GET 是空的:[_GET] => Array ()
  • “未定义变量:id”当您单击 check_button 时?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多