我正在尝试在插件中执行ajax请求。我所做的是:
Class Someclass {
public function __construct(){
}
public function current_page_function(){
add_action(
"wp_ajax_test_ajax",array($this,\'test_ajax\')
);
$this->data=array();
View_Includer::load(\'index\',$this->data,false,false,false,false,true,false,false);
}
public function test_ajax(){
wp_send_json_success(\'Science achieved!\');
exit();
}
}
脚本:
var send_ajax_request = function( cpt_id ) {
// Define the function and the data to send to the server.
var data = {
\'action\': \'test_ajax\',
\'post_type\': cpt_id
};
// Send the request to the server and handle the response.
jQuery.post( Essestials.ajaxurl, data, function( response ) {
return response;
// Handle your response here.
});
};
当我这样做时,wordpress返回400个错误代码。但当我做以下事情时:
Class Someclass {
public function __construct(){
add_action(
"wp_ajax_test_ajax",array($this,\'test_ajax\')
);
}
public function current_page_function(){
$this->data=array();
View_Includer::load(\'index\',$this->data,false,false,false,false,true,false,false);
}
public function test_ajax(){
wp_send_json_success(\'Science achieved!\');
exit();
}
}
它返回以下响应:
{success: true, data: "Science achieved!"}
我在第一个过程中做错了什么?为什么只有在构造函数中添加钩子时它才起作用?我们非常感谢您的任何帮助。谢谢