admin管理员组

文章数量:1422254

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.

本文标签: pluginsAjax call doesn39t work in frontend but it39s working in backend (when I39m logged in)