Ajax函数输出一个"0"当我在最后使用die()时

ajax function outputs a "0" when I use die() at the end

本文关键字:quot 最后 die 一个 输出 Ajax 函数      更新时间:2023-09-26

我有一个php页面,它正在进行几个ajax调用。发出第一个ajax调用,成功后激活第二个ajax函数。每个函数的末尾都有die()。无论我做什么,die()总是向屏幕输出一个"0"。我尝试将die()从第一个ajax函数中注释出来,但是当我这样做时,它永远不会处理ajax请求。加载gif一直在旋转。当我注释掉第二个函数中的die()时,它两次输出"0"。我不知道为什么它一直打印到屏幕上。

这是第一个函数。

function json_info() {
    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_REQUEST) ) {
    // create a new array to store projects
    $projectsArray = array();
    // get values for all three drop-down menus
    $status = $_REQUEST['status'];
    $industry = $_REQUEST['services'];
    $state = $_REQUEST['state'];
    // array of values for earch of the three drop-downs
    $statusAll = array('complete','incomplete');
    $industryAll = array('mining','textile','construction');
    $statesAll = array('sc','tx','wa');
    // set $statusArray dependent on whether or not "all" is selected
    if($status == "all") {
        $statusArray = array( 'key' => 'status', 'value' => $statusAll, 'compare' => 'IN');
    } else {
        $statusArray = array( 'key' => 'status', 'value' => $status, 'compare' => '=');
    }
    if($industry == "all") {
        $industryArray = array( 'key' => 'industry', 'value' => $industryAll, 'compare' => 'IN');
    } else {
        $industryArray = array( 'key' => 'industry', 'value' => $industry, 'compare' => '=');
    }
    if($state == "all") {
        $stateArray = array( 'key' => 'state', 'value' => $statesAll, 'compare' => 'IN');
    } else {
        $stateArray = array( 'key' => 'state', 'value' => $state, 'compare' => '=');
    }

        $pages = array(
            'post_type' => 'page',
            'orderby' => 'title',
            'order' => 'ASC',
            'paged' => $paged,
            'posts_per_page' => 5,
            'meta_query'    => array(
                                    'relation'      => 'AND',
                                    $statusArray,
                                    $industryArray,
                                    $stateArray,
                                        array(
                                        'key'       => '_wp_page_template',
                                        'value'     => 'template-individual-project.php',
                                        'compare'   => '='
                                    )
                                )
        );
        // query results by page template
        $my_query = new WP_Query($pages);
        $projectsArray = array();

        if($my_query->have_posts()) : 
                while($my_query->have_posts()) : 
                    $my_query->the_post(); 
                    $image = get_field('project_photo');
                    $image = $image['sizes']['thumbnail'];  
                    $projectsArray[] = array(
                    'title' => get_the_title(),
                    'lat' => get_field('latitude'),
                    'long' => get_field('longitude'),
                    'status' => get_field('status'),
                    'industry' => get_field('industry'),
                    'state' => get_field('state'),
                    'link' => get_permalink(),
                    'photo' => $image,
                    'num' => $paged
                    );  
            endwhile; endif;
            wp_reset_query();
         } // end of isset
         ?>
         <?php
         echo json_encode($projectsArray);
    // Always die in functions echoing ajax content
   die();
}
add_action( 'wp_ajax_json_info', 'json_info' );
add_action( 'wp_ajax_nopriv_json_info', 'json_info' );

这是第二个函数:

function json_info2() {
    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_REQUEST) ) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    // get values for all three drop-down menus
    $status = $_REQUEST['status'];
    $industry = $_REQUEST['services'];
    $state = $_REQUEST['state']; 
    // array of values for earch of the three drop-downs
    $statusAll = array('complete','incomplete');
    $industryAll = array('mining','textile','construction');
    $statesAll = array('sc','tx','wa');
    // set $statusArray dependent on whether or not "all" is selected
    if($status == "all") {
        $statusArray = array( 'key' => 'status', 'value' => $statusAll, 'compare' => 'IN');
    } else {
        $statusArray = array( 'key' => 'status', 'value' => $status, 'compare' => '=');
    }
    if($industry == "all") {
        $industryArray = array( 'key' => 'industry', 'value' => $industryAll, 'compare' => 'IN');
    } else {
        $industryArray = array( 'key' => 'industry', 'value' => $industry, 'compare' => '=');
    }
    if($state == "all") {
        $stateArray = array( 'key' => 'state', 'value' => $statesAll, 'compare' => 'IN');
    } else {
        $stateArray = array( 'key' => 'state', 'value' => $state, 'compare' => '=');
    }

        $pages = array(
            'post_type' => 'page',
            'orderby' => 'title',
            'order' => 'ASC',
            'paged' => $paged,
            'posts_per_page' => 5,
            'meta_query'    => array(
                                    'relation'      => 'AND',
                                    $statusArray,
                                    $industryArray,
                                    $stateArray,    
                                        array(
                                        'key'       => '_wp_page_template',
                                        'value'     => 'template-individual-project.php',
                                        'compare'   => '='
                                    )
                                )
        );
        // query results by page template
        $my_query = new WP_Query($pages);
        if($my_query->have_posts()) : 
                while($my_query->have_posts()) : 
                    $my_query->the_post();  
                    ?>  

                    <li class="group">
                        <?php the_title(); ?>   
                    </li>
                    <?php
            endwhile;endif;
            wp_reset_query();
         } // end of isset
         ?>
         <?php
    // Always die in functions echoing ajax content
   die();
}
add_action( 'wp_ajax_json_info2', 'json_info2' );
add_action( 'wp_ajax_nopriv_json_info2', 'json_info2' );

这是对两个函数的ajax调用:

function run_ajax() {
    // Get values from all three dropdown menus
        var state = $('#states').val();
        var markets = $('#markets').val();
        var services = $('#services').val();
        // This does the ajax request
        $.ajax({
            url: ajaxurl, 
            data: {
                'action' : 'json_info',
                'state' : state,
                'status' : markets,
                'services' : services
            },
            success:function(data) {
                // This outputs the result of the ajax request
                var jsonData = JSON.parse(data);
                do_ajax();
            }   /*,
            error: function(errorThrown){
                console.log(errorThrown);
            }*/
        }); // end of ajax call for json_info

        function do_ajax() {
            $.ajax({
                url: ajaxurl, 
                data: {
                    'action' : 'json_info2',
                    'state' : state,
                    'status' : markets,
                    'services' : services
                },
                success:function(moredata) {
                    // This outputs the result of the ajax request
                    $('#project-list').html( moredata );
                    $('#project-list').fadeIn();
                }/*,
                error: function(errorThrown){
                    var errorMsg = "No results match your criteria";
                    $('#project-list').html(errorMsg);
                }*/
            }); // end of ajax call
        } // end of function do_ajax
}

我不确定我错过了什么或做错了什么,导致"0"打印到屏幕

好吧,原来我是个白痴,我只是忽略了这个问题。ajax还调用了第三个函数,在它的末尾没有die()。我认为这个问题是在我的第二个函数中,因为0出现在我打印该内容的div中。