WordPress AJAX始终返回0

时间:2017-01-18 作者:Liam O\'Neill

我正在尝试通过ajax点击获取更多帖子。在我的函数中,我对脚本进行了本地化

wp_enqueue_script( \'news\', get_template_directory_uri().\'/news/js/news.js\', \'\', \'\', true );

//Localise script for ajax call
wp_localize_script( \'news\', \'ajax_posts\', array(
        \'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
        \'noposts\' => \'No Older Posts Found\',
));
我的帖子查询功能

function more_post_ajax(){

    $ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 2;
    $page = (isset($_POST[\'pageNumber\'])) ? $_POST[\'pageNumber\'] : 0;


    header("Content-Type: text/html");

    $args = array(
        \'suppress_filters\' => true,
        \'post_type\' => \'post\',
        \'posts_per_page\' => $ppp,
        \'paged\'    => $page
    );

    $loop = new WP_Query($args);

    $out = \'\';

    if ($loop -> have_posts()) :  while ($loop -> have_posts()) : $loop -> the_post();
        $out .=\'<article class="post-container clearfix">
                    <div class="post-inner clearfix">

                    <div class="post-image">
                        \'.get_the_post_thumbnail().\'
                    </div>
                    <h1>\'.the_title().\'</h1>
                    <span class="date">Date: \'.get_the_date().\'</span>
                    <br>
                    <span class="author">Author: \'.the_author().\'</span>
                    <br>
                    \'.get_custom_excerpt(get_the_content()).\'
                    <a class="read-more" href="\'.get_the_permalink().\'" title="Read More"><img src="\'.get_template_directory_uri().\'/news/img/read-more.png" alt="Read More"></a>
                    </div>
                </article>\';
    endwhile;
    endif;
    wp_reset_postdata();
    echo $out;
}

add_action(\'wp_ajax_nopriv_more_post_ajax\', \'more_post_ajax\');
add_action(\'wp_ajax_more_post_ajax\', \'more_post_ajax\');
然后是我的jQuery

jQuery(document).ready(function(){

    var ppp = 2; // Post per page
    var pageNumber = 1;


    function load_posts(){
        pageNumber++;
        var str = \'&pageNumber=\' + pageNumber + \'&ppp=\' + ppp + \'&action=more_post_ajax\';
        jQuery.ajax({
            type: "POST",
            dataType: "html",
            url: ajax_posts.ajaxurl,
            data: str,
            success: function(data){

                console.log(data);

                var $data = jQuery(data);
                if($data.length){
                    jQuery(".posts-wrapper").append($data);
                    jQuery(".load-more").attr("disabled",false);
                } else{
                    jQuery(".load-more").attr("disabled",true);
                }
            },
            error : function(jqXHR, textStatus, errorThrown) {
                $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }

        });
        return false;
    }

    jQuery(".load-more").on("click",function(e){ // When btn is pressed.

        jQuery(".load-more").attr("disabled",true); // Disable the button, temp.
        load_posts();

        e.preventDefault();

    });

});
ajax响应始终为0。谁能帮忙吗。

2 个回复
SO网友:Anton Pedan

在PHP函数的末尾尝试die()函数。这会有帮助的。

UPD:Ive made some simple version of your problem code to check it and it works. I think it will helps you. You are able to change inner PHP script to your logic and it wont返回0。

PHP:

function more_post_ajax(){
  echo "Hello";
  die();
}
add_action(\'wp_ajax_nopriv_more_post_ajax\', \'more_post_ajax\');
add_action(\'wp_ajax_more_post_ajax\', \'more_post_ajax\');

function custom_scripts_init(){
  wp_enqueue_script( \'custom\', get_template_directory_uri().\'/assets/js/custom.js\', array(\'jquery\') );
  wp_localize_script( \'custom\', \'ajaxPosts\', array(
    \'customUrl\' => admin_url( \'admin-ajax.php\' ),
    \'noposts\' => \'No Older Posts Found\',
));

}
add_action(\'wp_footer\', \'custom_scripts_init\');
JQuery:

jQuery(document).ready(function(){
jQuery("#page").append("<button type=\'button\' class=\'load-more\'/>");
var ppp = 2; // Post per page
var pageNumber = 1;


function load_posts(){
    pageNumber++;
    var str = \'&pageNumber=\' + pageNumber + \'&ppp=\' + ppp + \'&action=more_post_ajax\';
    jQuery.ajax({
        type: "POST",
        dataType: "html",
        url: ajaxPosts.customUrl,
        data: str,
        success: function(data){
            console.log(data);
        },
        error : function(jqXHR, textStatus, errorThrown) {
            //$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
        }

    });
    return false;
}

jQuery(".load-more").on("click",function(e){ // When btn is pressed.

   // jQuery(".load-more").attr("disabled",true); // Disable the button, temp.
    load_posts();

    e.preventDefault();

});
});

SO网友:Liam O\'Neill

如果有人谈到这一点,我只是想补充一下问题所在。

我从另一个开发人员那里继承了这份工作,出于我永远无法理解的原因,函数文件的顶部有ob\\u start。这导致ajax请求失败。

谢谢

相关推荐

自定义模板earch.php中的搜索结果

现在我在搜索结果页面(search.php)中遇到了一个问题,结果是由索引页面模板显示的,而不是它的模板,它显示所有帖子,不管搜索者是否输入了内容我的索引。php页面<?php get_header(); ?> <?php $search = $_get[\'search\']; $args = array( \'post_title_like\' => $search ); $res = new wp_query($args