【问题标题】:WordPress: Ajax response overwrites the whole pageWordPress:Ajax 响应覆盖整个页面
【发布时间】:2023-03-16 19:30:02
【问题描述】:

我最近开始使用 WordPress,并做了一个关于 Ajax 的小教程。

目前,我正在尝试检索帖子的内容,然后将它们显示在另一个 div 的同一页面上(已使用短代码创建)。一切正常,除了响应显示在整个页面上并且未显示在所选 div 中 - 从而清除其他内容。

请在下面找到我写的代码的sn-ps:

 ////CREATING SHORT CODES
        public function posts_callback($atts = null, $content = null) {
        $args = array("post_type" => "post", "orderby" => "date", "order" => "DESC", "post_status" => "publish");
        $posts = new WP_Query($args);
        ?>
        <div style ="text-align: center" >
            <?php
            if ($posts->have_posts()):
                while ($posts->have_posts()):
                    $posts->the_post();
                    $link = admin_url('admin-ajax.php?action=post_content&post_id='.get_the_ID());
                    ?>
                    <div id="posts_callback_div" style="display: inline-block; width: 300px; border-color: #333; border-style: solid; border-width: 2px; margin-top: 15px;">        
                        <a class='short_code_link' data-post-id='<?php echo get_the_ID() ?>' href='<?php echo $link ?>' >
                            <?php echo get_the_title(); ?>
                        </a>
                    </div>
                    <?php
                endwhile;
            else:
                echo "";
                die();
            endif;
            ?>
        </div>
        <?php
    }

    public function custom_container_shortCode() {
        ?>
        <div class="custom_container" id="custom_container"  style="width: 300px; border-color: #333; border-style: solid; border-width: 2px; margin-top: 15px;">
            Hi, if you can still see me then the ajax is not working!
        </div>
        <?php
    }

//JQUERY部分

(function ($) {
    'use strict';
    $(".short_code_link").click(function (e) {
        e.preventDefault();
        postID = jQuery(this).attr("data-post-id");
        jQuery.ajax({
            type: "GET",
            url: myAjax.ajaxurl,
           // dataType: "html",
            cache: false,
            data: {
                action: "post_content",
                post_id: postID,
            },
            success: function (response) {
                jQuery("#custom_container").html(response);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus);
                alert("Error: " + errorThrown);
            }
        });
    });
})(jQuery);

//PHP 处理程序

//CREATING AJAX API TO RETRIEVE POST CONTENT
public function post_content() {
    $post_id        = $_GET["post_id"];
    $content_post   = get_post($post_id);
    $content        = $content_post->post_content;
    $content        = apply_filters('the_content', $content);
    $content        = str_replace(']]>', ']]$gt', $content);
    echo $content;
    die();
}

你能指出我做错了什么吗?

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

    您对服务器进行异步调用并返回一些html。成功后,您将使用从服务器返回的任何内容替换元素的所有内容为 id="custom_container"

    这是执行此操作的概述功能:

           ...
           success: function (response) {
                jQuery("#custom_container").html(response);
            },
           ...
    

    您可能希望将#custom_container 替换为另一个选择器,具体取决于您要放置收到的html 的位置。

    请注意,新创建的html 不会被初始化。如果您在页面加载时有任何脚本将事件/行为绑定到元素,则需要将事件/行为添加到上面概述的 success 函数内新添加的 html,或者您需要在页面加载时使用通用绑定.

    【讨论】:

    • 谢谢。我只想替换#custom_container 中的内容。但是整个页面都被响应替换了,我不明白为什么。因此,我的问题是要了解为什么会发生这种情况。您能否详细说明事件/行为部分?
    • 在这种情况下,只有一个选项可以想到:您可能有两个 id 为 custom_container 的元素,并且只有在 DOM 树(父级)中遇到的第一个元素被首先替换。第二个永远不会被解析,因为它已被删除。请检查。
    • 感谢您的意见。我实际上使用的是 wordpress 样板文件,并且我在同一个管理脚本文件中插入了 ajax jquery 函数,该文件已经包含另一个 ajax 函数。这是不正确的吗?我应该在另一个脚本文件中创建第二个 ajax 函数并将其排入队列吗?还是这不影响它?此外,只有一个元素具有此 ID。
    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2011-02-01
    • 2020-04-17
    • 2016-08-28
    相关资源
    最近更新 更多