最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

load ajax using admin-ajax.php

programmeradmin0浏览0评论

The append result is 0, instead of a list with the title ass a link. If I check this locally (http://localhost/wordpress/wp-admin/admin-ajax.php?action=lateral_fluid), I get the titles ass a link. I don't know why I get the appended 0. What am I missing?

FUNCTIONS.PHP

// Your actual AJAX script
wp_enqueue_script('lateral-fluid', get_template_directory_uri().'/js/lateral-fluid-ajax.js', array('jquery'));

// This will localize the link for the ajax url to your 'my-script' js file (above). You can retreive it in 'lateral-fluid.js' with 'lateral-fluid.ajaxurl'
wp_localize_script( 'lateral-fluid', 'ajaxFluid', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

PHP FILE

add_action( 'wp_ajax_nopriv_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_lateral_fluid', 'my_lateral_fluid' );

function my_lateral_fluid() {
    $args = array(
        "post_type" => "portfolio",
        "posts_per_page" => -1  
    );

    $posts_array = get_posts($args);

    echo '<nav role="navigation">';

    echo '<h2>Latest News</h2>';

    echo '<ul>';

    foreach ($posts_array as $post):
        setup_postdata($post);
        echo '<li><a class="' . esc_attr("side-section__link") . '" href="' . esc_attr(get_page_link($post->ID)) . '">' . $post->post_title . '</a>';
    endforeach;

    echo '</ul>';

    echo '</nav>';

        wp_die();
}

JS FILE

jQuery.ajax({
        type: 'get',
        url: ajaxFluid.ajaxurl,
        data: {
                action: 'my_lateral_fluid', // the PHP function to run
        },
        success: function(data, textStatus, XMLHttpRequest) {
                jQuery('#portfolio').html(''); // empty an element
                jQuery('#portfolio').append(data); // put our list of links into it

        },

        error: function(XMLHttpRequest, textStatus, errorThrown) {
                if(typeof console === "undefined") {
                        console = {
                                log: function() { },
                                debug: function() { },
                        };
                }
                if (XMLHttpRequest.status == 404) {
                        console.log('Element not found.');
                } else {
                        console.log('Error: ' + errorThrown);
                }
        }
});

The append result is 0, instead of a list with the title ass a link. If I check this locally (http://localhost/wordpress/wp-admin/admin-ajax.php?action=lateral_fluid), I get the titles ass a link. I don't know why I get the appended 0. What am I missing?

FUNCTIONS.PHP

// Your actual AJAX script
wp_enqueue_script('lateral-fluid', get_template_directory_uri().'/js/lateral-fluid-ajax.js', array('jquery'));

// This will localize the link for the ajax url to your 'my-script' js file (above). You can retreive it in 'lateral-fluid.js' with 'lateral-fluid.ajaxurl'
wp_localize_script( 'lateral-fluid', 'ajaxFluid', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

PHP FILE

add_action( 'wp_ajax_nopriv_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_lateral_fluid', 'my_lateral_fluid' );

function my_lateral_fluid() {
    $args = array(
        "post_type" => "portfolio",
        "posts_per_page" => -1  
    );

    $posts_array = get_posts($args);

    echo '<nav role="navigation">';

    echo '<h2>Latest News</h2>';

    echo '<ul>';

    foreach ($posts_array as $post):
        setup_postdata($post);
        echo '<li><a class="' . esc_attr("side-section__link") . '" href="' . esc_attr(get_page_link($post->ID)) . '">' . $post->post_title . '</a>';
    endforeach;

    echo '</ul>';

    echo '</nav>';

        wp_die();
}

JS FILE

jQuery.ajax({
        type: 'get',
        url: ajaxFluid.ajaxurl,
        data: {
                action: 'my_lateral_fluid', // the PHP function to run
        },
        success: function(data, textStatus, XMLHttpRequest) {
                jQuery('#portfolio').html(''); // empty an element
                jQuery('#portfolio').append(data); // put our list of links into it

        },

        error: function(XMLHttpRequest, textStatus, errorThrown) {
                if(typeof console === "undefined") {
                        console = {
                                log: function() { },
                                debug: function() { },
                        };
                }
                if (XMLHttpRequest.status == 404) {
                        console.log('Element not found.');
                } else {
                        console.log('Error: ' + errorThrown);
                }
        }
});
Share Improve this question edited Jan 12, 2016 at 19:03 Pablo Levin asked Jan 12, 2016 at 18:42 Pablo LevinPablo Levin 2894 silver badges13 bronze badges 8
  • doesn't seems like you registered your function as a handler with something like add_action('wp_ajax_my_action','my_action_handler') – Mark Kaplun Commented Jan 12, 2016 at 18:54
  • Sorry I forgot to add to explanation. Already edited. – Pablo Levin Commented Jan 12, 2016 at 18:58
  • I don't understand why I append a 0. – Pablo Levin Commented Jan 12, 2016 at 19:00
  • Have you tried a simple exit; or die; instead of wp_die();? – s_ha_dum Commented Jan 12, 2016 at 19:15
  • It doesn't work. I try both. – Pablo Levin Commented Jan 12, 2016 at 19:17
 |  Show 3 more comments

3 Answers 3

Reset to default 1

The codex says here that The wp_ajax_ hook follows the format "wp_ajax_$youraction", where $youraction is your AJAX request's 'action' property.

I suppose in your js file you should go for:

data: {
            action: 'lateral_fluid', // the $action to run
      }

This was wrong:

Replace this:

add_action( 'wp_ajax_nopriv_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_lateral_fluid', 'my_lateral_fluid' );

With this:

add_action( 'wp_ajax_nopriv_my_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_my_lateral_fluid', 'my_lateral_fluid' );

The action parameter in data array is NOT the function to run, it's the action where you hook your function. So, basically changing

add_action( 'wp_ajax_nopriv_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_lateral_fluid', 'my_lateral_fluid' );  

to

add_action( 'wp_ajax_nopriv_my_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_my_lateral_fluid', 'my_lateral_fluid' );  

Will do the trick. NOTE the change in the hook names. For more information on how ajax works in WordPress please check https://codex.wordpress/AJAX_in_Plugins

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论