add action in class not running, always return 0

I’m trying to run an ajax action in my custom class but it seems to not call at all the action (class method actually). The scripts are well loaded, means the class is included and instanciated.

class Myclass {

    public function __construct() {

        add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
        add_action( 'wp_ajax_loadmore', array( $this, 'loadmore' ) );
        add_action( 'wp_ajax_nopriv_loadmore', array( $this, 'loadmore' ) );

    }

    public function scripts() {

        wp_register_script( 'filter', 'assets/js/filter.js', array( 'jquery' ), null, true );
        wp_enqueue_script( 'filter' );
        wp_localize_script( 'filter', 'filter', array(
            'ajaxurl'        => admin_url( 'admin-ajax.php' ),
            'loadmore_nonce' => wp_create_nonce( 'loadmore_nonce' )
        ) );

    }

    public function loadmore() {

        if ( ! check_ajax_referer( 'loadmore_nonce', 'loadmore_nonce', false ) )
            exit;

        @header( 'Content-Type: application/json' );
        echo json_encode( array(
            'foo' => 'bar'
        ) );
        exit;


    }

}

In my JS file I wrote the following:

Read More
$('#loadmore a').on('click', function(e) {

    e.preventDefault();

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: filter.ajaxurl,
        data: {
            'action': 'loadmore',
            'loadmore_nonce': filter.loadmore_nonce
        },
        success: function (response) {

            console.log(response);

        }
    });

});

The AJAX success callback always returns 0

And the 0 comes from die(‘0’); at the end of the admin-ajax.php file

Related posts