【问题标题】:ajax POST data results are in the console, but displayed on the page?ajax POST 数据结果在控制台,但显示在页面上?
【发布时间】:2018-07-09 17:32:46
【问题描述】:

直到我发现它实际上是一个问题才开始出现这个问题。我为这个令人困惑的问题道歉。基本上,我正在用 php 收集一个 mysql id,并使用 ajax 的 POST 方法尝试将该 post id 放入查询中。问题是查询有效并且数据显示在控制台上,但不在屏幕上显示:

在屏幕本身上,除了 array(0){},我什么也没看到。因为var_dump。 几乎似乎它没有在屏幕上更新,但它在控制台上。这是我的模态代码的样子:

<div class='modal fade' id='small' role='dialog'>
<div class='modal-dialog'>
<div class='modal-content'>
    <h3 class="fetchuserid" style='text-align: center;'>
    <?php

        $db = mysqli_connect("localhost", "root", "password", "testdb");



        $uid = $_POST['postuserid'];
        var_dump($_POST); <-- This is where you should be seeing the array(1){["postuserid"] => string(2) "10"} `10` being the id I clicked.


        $ssql = "SELECT * FROM foodid WHERE postid=$uid";
        $rresult = mysqli_query($db,$ssql);
        while($lrow = mysqli_fetch_assoc($rresult)){

            echo $lrow['countid'];
            echo '<span class="postid_count">'.$lrow['countid'].'</span>'; <-- This is where you should be seeing all of those span in the console.

        }

    ?>
    </h3>
    </div>
</div>
</div>

这里是访问modal的锚标签,负责保存id:

echo '<a  href="#" data-postuserid="'.$row['id'].'" id="smallbtn" name="smallbtn" data-toggle="modal" data-target="#small"  data-modal="small">';

最后是我的 ajax:

$(document).on('click','#smallbtn',function(){

        var postuserid = $(this).data('postuserid');
            $post = $(this);
            console.log($(this).data('postuserid'));

        $.ajax({

            url: 'cooking.php',
            type: 'POST',
            data: {
                'postuserid' : postuserid
            },
             success:function(data)
        {
            console.log(data);
            $post.parent().find('span.postid_count').text(data); <-- Here is what I tried to do to I guess "update" the data from the POST. No success.
             return data; <-- I read other questions and was told to do this. No success.
        },
        error:function(data)
        {
           $( document ).ajaxError(function( event, request, settings )
            {
              $("#banner-message > p").text('Sorry, we are having a trouble in this url "'+settings.url+'!"') 
            });
        }
        });
    });

注意:这一切都发生在一个页面中,没有刷新,因此没有数据丢失。我确实检查了我的网络选项卡,看看这是否是问题所在,并且使用 200 POST 方法一切正常。它还在网络和控制台中显示了正确的 postuserid。

我不确定我在这里问的问题是否正确,但欢迎任何反馈!基本上我怎样才能从控制台的 ajax 帖子中显示我的查询结果并将其放在页面上?

笔:codepen.io/anon/pen/djbgKj

【问题讨论】:

  • 第一次运行你的 php,它将导致空的 var 转储,因为没有传递 POST 参数。然后,当 ajax 启动时,它会传递 POST 参数......嗯............不知何故,这个问题看起来非常熟悉。
  • 没错。既然你这么说了。我认为 php 只是在您进入页面后立即运行?但话又说回来,我在 post 方法显示在模态之前激活它。
  • cooking.php 是否与使用 ajax javascript 块生成原始页面的 php 相同?当我认为您只想要跨度列表时,似乎会通过该 ajax 调用返回大量 html。
  • @IncredibleHat 我不确定你的意思。但是大部分 HTML 是从 php 查询中自动生成的。

标签: javascript php jquery ajax post


【解决方案1】:

这对你有帮助

document.getElementById("some_id").innerHTML = data;

请试一试

在你的情况下,你可以这样做:

$post.parent().find('span.postid_count').innerHTML = data;

【讨论】:

    【解决方案2】:

    听起来您没有正确定位所需的 HTML 元素,或者它不存在:

    试试:

    alert( $post.parent().find('span.postid_count').length ); // or use console.log() if you prefer, it doesn't matter
    

    我希望您收到 1 个或更多的警报。如果没有,那么您需要正确定位页面中的 HTML 元素。

    另外使用.html() 而不是.text(),因为.text() 会从您的data 字符串中去除HTML 代码。

    $post.parent().find('span.postid_count').html(data);
    

    【讨论】:

    • 感谢您提及。我不知道发生了什么或为什么。另外,我尝试并使用了这两个代码,并得到 0 作为警报的结果。这意味着我没有以正确的 HTML 元素为目标? @MonkeyZeus
    • @LaneyWilliams 正确,$post.parent().find('span.postid_count') 没有返回匹配的元素,因此您的数据无处可去。
    • 我还要注意,在我的 Pen 中,我有相同的 ajax 代码,但 HTML 不同。它会正确更新该金额。
    • 那么$post.parent().find('span.postid_count') 到底在说什么?它是否试图找到作为父级的 data-userpostid 并将数据提供给跨度?
    • @LaneyWilliams CodePen 不执行 PHP,因此它被视为纯 HTML。当您在服务器上执行代码时,您会得到不同的 HTML。如果您使用的是 Google Chrome,则运行您的脚本并按 ctrl + u 并验证您的 HTML 是否正是您认为的那样。
    【解决方案3】:

    改变这个:

    $post.parent().find('span.postid_count').text(data);

    到这里:

    $post.parent().find('span.postid_count').html(data);

    让我知道它是否适合你。

    【讨论】:

    • 好吧,我把它关掉了,我还是什么也没得到。
    • 好的,请您发布您自动生成的全部 HTML 内容。看起来您的 jQuery 选择器无法找到正确的元素。
    • 我相信上面的模态代码就是生成的html内容?它是在控制台图像中显示所有这些跨度的内容。
    • 一个大问题是初始生成不会创建任何 span.postid_count 以在 ajax 从按钮单击返回后随后替换。需要有一个容器来交换所有跨度。我看到的唯一容器是h3 class="fetchuserid"
    • 我说的是围绕你的#smallbtn 的 html - 正如 IncredibleHat 所说,就像这个按钮的 parent() 不存在,所以 jQuery 无法显示它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2020-03-11
    相关资源
    最近更新 更多