AJAX调用在前端不起作用,但在后端起作用(当我登录时)

时间:2019-06-27 作者:dragos.nicolae

电话如下:

make_ajax_call( \'<?php echo wp_create_nonce( \'tidplus-ajax-nonce\' ); ?>\', MyAjax.ajaxurl, \'section-frontend\', \'page-ticket\' );
这是。js代码:

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);
        }
    )
}
当我登录时,它工作正常,变量被发送到数据库中保存并以表单返回。注销时,它不会保存任何内容。控制台中似乎有一个管理ajax。php 400错误的请求(但console.log(ajaxurl)显示了管理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();
    }

1 个回复
最合适的回答,由SO网友:Bhupen 整理而成

您没有添加wp\\u ajax\\u nopriv,这就是未登录用户ajax失败的原因。

public function register() {
    add_action( \'wp_ajax_tidplus\' , array( $this, \'post\' ) );
    add_action( \'wp_ajax_nopriv_tidplus\' , array( $this, \'post\' ) );
}
请进行更改并检查。

相关推荐

将所有“非PHP”文件放在不同的服务器上

我想分开我的。wordpress使用的所有其他文件(图像、脚本等)中的php文件。所以我把它们放在不同的服务器上。我可以通过简单地将“siteurl”和“home”设置为文件服务器的url,使我的wordpress站点知道这些文件。这对图像来说很好,但破坏了其他一切(就像每个人都期望的那样)有人知道有更可行的方法吗?