电话如下:
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();
}