plugins - Ajax call doesn't work in frontend but it's working in backend (when I'm logged in)

Here is is the call:make_ajax_call( '<?php echo wp_create_nonce( 'tidplus-ajax-nonce' ); ?>',

Here is is the call:

make_ajax_call( '<?php echo wp_create_nonce( 'tidplus-ajax-nonce' ); ?>', MyAjax.ajaxurl, 'section-frontend', 'page-ticket' );

Here is the .js code:

function make_ajax_call( nonce, ajax_url, view_to_load, in_div, param1, param2, param3, param4  ) {  
//    var ajaxurl = ajax_url;
    jQuery( '#' + in_div ).block( { message: null, overlayCSS: { backgroundColor: '#f3f4f5' } });
    jQuery.post(
        ajaxurl,
        {
            'action': 'tidplus',
            'page': view_to_load,
            'response_div': in_div,
            'task': 'load_response',
            'nonce': nonce,
            'param1': param1,
            'param2': param2,
            'param3': param3,
            'param4': param4
        },
        function ( response ) {
            setTimeout(function () {
                jQuery( '#' + in_div ).unblock();
                if ( param2 == 'append' )
                    jQuery( '#' + in_div ).append( response );
                else
                    jQuery( '#' + in_div ).html( response );
            }, 500);
        }
    )
}

It is working fine when I'm logged in, the variables are sent into the database saved and returned in form. When logged out, it will not save anything. In console appears to be an admin-ajax.php 400 bad request (but the console.log(ajaxurl) shows the correct directoty of admin-ajax.php).

protected $page_to_load;

    public static $param1;
    public static $param2;
    public static $param3;
    public static $param4;

    public function register() {
        add_action( 'wp_ajax_tidplus' , array( $this, 'post' ) );
    }

    public function post() {
        $task = sanitize_text_field( $_POST['task'] );

        if ( isset ( $_POST['page'] ) )
            $this->page_to_load = sanitize_text_field( $_POST['page'] );

        if ( isset ( $_POST['param1'] ) )
            self::$param1 = sanitize_text_field( $_POST['param1'] );

        if ( isset ( $_POST['param2'] ) )
            self::$param2 = sanitize_text_field( $_POST['param2'] );

        if ( isset ( $_POST['param3'] ) )
            self::$param3 = sanitize_text_field( $_POST['param3'] );

        if ( isset ( $_POST['param4'] ) )
            self::$param4 = sanitize_text_field( $_POST['param4'] );

        $this->handle_ajax_posts( $task );
    }

    private function verify_ajax_nonce() {
        if ( isset( $_POST['nonce'] ) ) {
            if ( wp_verify_nonce( $_POST['nonce'], 'tidplus-ajax-nonce' ) ) {
                return true;
            }
            return false;
        }
        return false;
    }

    private function handle_ajax_posts( $task ) {
        if ( $task == 'load_modal_page' )
            $this->load_modal_page();

        if ($task == 'load_response') {
            if ( $this->verify_ajax_nonce() == true ) {
                $this->load_response();
            }
        }

    }

    private function load_modal_page() {
            switch ($this->page_to_load) {
                case 'confirm-action':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                case 'confirm-action-orders':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                default:
                    break;
            }
        die();
    }

    private function load_response() {
        switch ($this->page_to_load) {
                case 'confirm-action':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                case 'section-frontend':
                require ( "$this->plugin_path/templates/frontend/$this->page_to_load.php" );

                    break;



                default:

                    require ( "$this->plugin_path/templates/backend/tickets/$this->page_to_load.php") ;
                    break;
            }

        die();
    }

Here is is the call:

make_ajax_call( '<?php echo wp_create_nonce( 'tidplus-ajax-nonce' ); ?>', MyAjax.ajaxurl, 'section-frontend', 'page-ticket' );

Here is the .js code:

function make_ajax_call( nonce, ajax_url, view_to_load, in_div, param1, param2, param3, param4  ) {  
//    var ajaxurl = ajax_url;
    jQuery( '#' + in_div ).block( { message: null, overlayCSS: { backgroundColor: '#f3f4f5' } });
    jQuery.post(
        ajaxurl,
        {
            'action': 'tidplus',
            'page': view_to_load,
            'response_div': in_div,
            'task': 'load_response',
            'nonce': nonce,
            'param1': param1,
            'param2': param2,
            'param3': param3,
            'param4': param4
        },
        function ( response ) {
            setTimeout(function () {
                jQuery( '#' + in_div ).unblock();
                if ( param2 == 'append' )
                    jQuery( '#' + in_div ).append( response );
                else
                    jQuery( '#' + in_div ).html( response );
            }, 500);
        }
    )
}

It is working fine when I'm logged in, the variables are sent into the database saved and returned in form. When logged out, it will not save anything. In console appears to be an admin-ajax.php 400 bad request (but the console.log(ajaxurl) shows the correct directoty of admin-ajax.php).

protected $page_to_load;

    public static $param1;
    public static $param2;
    public static $param3;
    public static $param4;

    public function register() {
        add_action( 'wp_ajax_tidplus' , array( $this, 'post' ) );
    }

    public function post() {
        $task = sanitize_text_field( $_POST['task'] );

        if ( isset ( $_POST['page'] ) )
            $this->page_to_load = sanitize_text_field( $_POST['page'] );

        if ( isset ( $_POST['param1'] ) )
            self::$param1 = sanitize_text_field( $_POST['param1'] );

        if ( isset ( $_POST['param2'] ) )
            self::$param2 = sanitize_text_field( $_POST['param2'] );

        if ( isset ( $_POST['param3'] ) )
            self::$param3 = sanitize_text_field( $_POST['param3'] );

        if ( isset ( $_POST['param4'] ) )
            self::$param4 = sanitize_text_field( $_POST['param4'] );

        $this->handle_ajax_posts( $task );
    }

    private function verify_ajax_nonce() {
        if ( isset( $_POST['nonce'] ) ) {
            if ( wp_verify_nonce( $_POST['nonce'], 'tidplus-ajax-nonce' ) ) {
                return true;
            }
            return false;
        }
        return false;
    }

    private function handle_ajax_posts( $task ) {
        if ( $task == 'load_modal_page' )
            $this->load_modal_page();

        if ($task == 'load_response') {
            if ( $this->verify_ajax_nonce() == true ) {
                $this->load_response();
            }
        }

    }

    private function load_modal_page() {
            switch ($this->page_to_load) {
                case 'confirm-action':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                case 'confirm-action-orders':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                default:
                    break;
            }
        die();
    }

    private function load_response() {
        switch ($this->page_to_load) {
                case 'confirm-action':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                case 'section-frontend':
                require ( "$this->plugin_path/templates/frontend/$this->page_to_load.php" );

                    break;



                default:

                    require ( "$this->plugin_path/templates/backend/tickets/$this->page_to_load.php") ;
                    break;
            }

        die();
    }
Share Improve this question edited Jun 27, 2019 at 12:24 dragos.nicolae asked Jun 27, 2019 at 12:12 dragos.nicolaedragos.nicolae 134 bronze badges 2
  • 1 Please share ajax function you have created. – Bhupen Commented Jun 27, 2019 at 12:15
  • I've added (edited) the code executed by the ajax call – dragos.nicolae Commented Jun 27, 2019 at 12:24
Add a comment  | 

1 Answer 1

Reset to default 2

You have not added wp_ajax_nopriv so that is the reason for ajax failure for non-logged in users.

public function register() {
    add_action( 'wp_ajax_tidplus' , array( $this, 'post' ) );
    add_action( 'wp_ajax_nopriv_tidplus' , array( $this, 'post' ) );
}

Please do this change and check.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365237a4624546.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信