Mariusx
Mariusx

Reputation: 19

Simple wordpress ajax query is not working

Do you have any idea why this simple wp ajax query is not working? It always returns fail. Console -> 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

Upvotes: 0

Views: 386

Answers (3)

Akshay Shah
Akshay Shah

Reputation: 3504

There is another alternative option. I agree with samuel, but I am sharing one more option:

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

Upvotes: 1

Stan Chechun
Stan Chechun

Reputation: 11

Your action it's 'load_filter', Also you are must be localize ajaxurl use this function wp_localize_script

$.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");
        }
    });

Upvotes: 0

Ismail
Ismail

Reputation: 743

The action should be load-filter instead of prefix_load_cat_posts. Seeing your PHP code, the prefix_load_cat_posts is actually the callback function name.

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

Upvotes: 1

Related Questions