使用admin-ajax.php加载AJAX

时间:2016-01-12 作者:Pablo Levin

追加结果为0,而不是标题为ass链接的列表。如果我在本地检查(http://localhost/wordpress/wp-admin/admin-ajax.php?action=lateral_fluid), 我从链接中获得标题。我不知道为什么我会得到附加的0。我错过了什么?

功能。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文件

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文件

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

3 个回复
SO网友:urka_mazurka

抄本上说that wp\\u ajax\\uhook遵循格式“wp\\u ajax\\u$youraction”,其中$youraction是ajax请求的“action”属性。

我想在js文件中,您应该选择:

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

SO网友:Pablo Levin

这是错误的:

替换此:

add_action( \'wp_ajax_nopriv_lateral_fluid\', \'my_lateral_fluid\' );
add_action( \'wp_ajax_lateral_fluid\', \'my_lateral_fluid\' );
使用此选项:

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

SO网友:Abhik

这个action 数据数组中的参数不是要运行的函数,而是挂接函数的操作。所以,基本上改变了

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

add_action( \'wp_ajax_nopriv_my_lateral_fluid\', \'my_lateral_fluid\' );
add_action( \'wp_ajax_my_lateral_fluid\', \'my_lateral_fluid\' );  
会成功的。NOTE 钩子名称的更改。有关ajax如何在WordPress中工作的更多信息,请查看https://codex.wordpress.org/AJAX_in_Plugins