jQuery:单击Ajax返回数据,但在第一次调用时显示在警报中

jQuery: On Click Ajax return data but does show in alert on first call

本文关键字:调用 第一次 显示 单击 Ajax 返回 数据 jQuery      更新时间:2023-09-26

我有一些jquery代码。 当单击按钮时,它应该向服务器控制器操作发送 AJAX 调用,从那里获取一些数据并将其返回到 ID 为"note-popover"的"DIV"中。

.on('click.notes', '.pd-notes', function (e) {
        e.preventDefault();
        $.post("a_a/pd_notes", function (data) {
            $("#note-popover").html(data);
        });
        alert("Data Returned" + $("#note-popover").html());
    });

我也在Chrome开发工具中测试了此代码,在第一次单击时,它会调用服务器,但是警报内容显示为空,即使在DOM中"DIV"填充了返回的数据,但是每次连续调用警报似乎都会返回警报中的数据。

我是新来的,所以这可能是一个愚蠢的问题。这种行为的原因是什么以及如何解决它。

任何帮助不胜感激

谢谢

数据仅在处理程序中有效

.on('click.notes', '.pd-notes', function (e) {
        e.preventDefault();
        $.post("a_a/pd_notes", function (data) {
            $("#note-popover").html(data);
            alert("Data Returned" + $("#note-popover").html());
        });
    });

$.post 是异步的,并启动一个操作,在您的示例中立即返回到您的alert

稍后数据返回到您提供给 post 的回调函数。