简单的wordpress ajax查询不起作用

问题描述 投票:0回答:3

你知道为什么这个简单的 wp ajax 查询不起作用吗?它总是返回失败。控制台 -> https://pastebin.com/TABQCjXe

jQuery(document).ready(function($) {

// This does the ajax request
$.ajax({
    type: 'post',
    url: ajaxurl,
    data: {
        'action':'prefix_load_cat_posts'
    },
    success:function(data) {
        // This outputs the result of the ajax request
        console.log(data);
        $( ".prefix_load_cat_posts" ).append("success");
    },
    error: function(errorThrown){
        console.log(errorThrown);
        $( ".prefix_load_cat_posts" ).append("fail");
    }
});

});

PHP -> https://pastebin.com/g4QiWDky

ajax wordpress admin-ajax
3个回答
1
投票

动作应该是

load-filter
而不是
prefix_load_cat_posts
。查看您的 PHP 代码,
prefix_load_cat_posts
实际上是回调函数名称。

data: {
    'action':'load-filter'
},

1
投票

还有另一种选择。我同意塞缪尔的观点,但我分享另一个选择:

add_action( 'wp_ajax_nopriv_prefix_load_cat_posts', 'prefix_load_cat_posts' );
add_action( 'wp_ajax_prefix_load_cat_posts', 'prefix_load_cat_posts' );

0
投票

你的操作是'load_filter',而且你必须使用这个函数wp_localize_script本地化ajaxurl

$.ajax({
        type: 'post',
        url: ajaxurl,
        data: {
            'action':'load-filter'
        },
        success:function(data) {
            // This outputs the result of the ajax request
            console.log(data);
            $( ".prefix_load_cat_posts" ).append("success");
        },
        error: function(errorThrown){
            console.log(errorThrown);
            $( ".prefix_load_cat_posts" ).append("fail");
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.