简体   繁体   English

当我在最后使用die()时,ajax函数输出“ 0”

[英]ajax function outputs a “0” when I use die() at the end

I have a php page that is making a couple of ajax calls. 我有一个PHP页面正在执行几个Ajax调用。 The first ajax call is made and on success it activates a second ajax function. 进行第一个ajax调用,并成功调用它来激活第二个ajax函数。 Each function has die() at the end. 每个函数的末尾都有die()。 No matter what I do, die() keeps outputting a "0" to the screen. 无论我做什么,die()都会一直在屏幕上输出“ 0”。 I tried commenting the die() out of the first ajax function, but it never processes the ajax request when I do that. 我尝试在第一个ajax函数中注释die(),但是当我这样做时,它从不处理ajax请求。 The loading gif just keeps spinning. 加载gif一直在旋转。 When I comment out the die() in the second function, it outputs "0" twice. 当我在第二个函数中注释出die()时,它两次输出“ 0”。 I have no clue why it keeps printing that to the screen. 我不知道为什么它一直将其打印到屏幕上。

This is the first function. 这是第一个功能。

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' );

And this is the second function: 这是第二个功能:

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' );

And this is the ajax call to both functions: 这是对两个函数的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
}

I'm not sure what I'm missing or doing wrong that is causing the "0" to print to the screen. 我不确定我丢失了什么或做错了什么导致“ 0”打印到屏幕上。

Well, it turns out that I'm an idiot, and I was just overlooking the problem. 好吧,事实证明我是个白痴,而我只是忽略了这个问题。 There was a third function called by ajax, and I didn't have die() at the end of it. ajax调用了第三个函数,而在它的末尾没有die()。 I was thinking the issue was in my second function since the 0 was showing up in the div where I was printing that content. 我以为问题出在我的第二个函数中,因为0出现在打印该内容的div中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM