为什么我的ajax函数不能按预期工作?
所以我的班级看起来是这样的:为什么我的ajax函数不能按预期工作?,ajax,wordpress,hook,Ajax,Wordpress,Hook,所以我的班级看起来是这样的: class Myclass{ private $nonce; public function __construct(){ if( get_current_screen()->id == 'nav-menus' ){ $this->nonce = 'my-plugin-nonce'; } add_action( 'wp_ajax_run_action', arra
class Myclass{
private $nonce;
public function __construct(){
if( get_current_screen()->id == 'nav-menus' ){
$this->nonce = 'my-plugin-nonce';
}
add_action( 'wp_ajax_run_action', array( $this, 'run' ) );
wp_localize_script(
'my-script',
'my_script',
array( 'nonce' => wp_create_nonce( $this->nonce ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
public function run(){
if( ! wp_verify_nonce( $_POST['nonce'], $this->nonce ) )
return false;
wp_send_json_success();
}
}
new Myclass;
下面是javascript代码:
$.ajax({
type: 'POST',
dataType: 'json',
url: my_script.ajaxurl,
data: {
'action': 'run_action',
'nonce' : my_script.nonce,
},
complete: function( object ) {
console.log( object.responseJSON )
}
});
问题是,当我试图从javascript ajax函数中调用run\u操作时,它不会像应该的那样返回true
请注意,我已经正确地本地化了脚本,并且对象中包含的任何数据都被正确地呈现
为什么会发生这种情况?法典中的注释:
必须在使用wp\u register\u script()
或wp\u enqueue\u script()注册脚本后调用wp\u localize\u script()
因此,您的工作流程应如下所示:
注册脚本
本地化
将本地化脚本排队
将其绑定到适当的操作:wp\u排队\u脚本
或admin\u排队\u脚本
例如:
将操作添加到您的\u构造中
方法:
add_action('wp_enqueue_scripts', array($this, 'registerAjaxScript'));
然后创建一个注册和本地化脚本的方法:
function registerAjaxScript() {
wp_register_script('my-script',
string $src,
array $deps,
string or bool $ver,
bool $in_footer
);
wp_localize_script('my-script',
'my_script',
array( 'nonce' => wp_create_nonce( $this->nonce ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
法典中的注释:
必须在使用wp\u register\u script()
或wp\u enqueue\u script()注册脚本后调用wp\u localize\u script()
因此,您的工作流程应如下所示:
注册脚本
本地化
将本地化脚本排队
将其绑定到适当的操作:wp\u排队\u脚本
或admin\u排队\u脚本
例如:
将操作添加到您的\u构造中
方法:
add_action('wp_enqueue_scripts', array($this, 'registerAjaxScript'));
然后创建一个注册和本地化脚本的方法:
function registerAjaxScript() {
wp_register_script('my-script',
string $src,
array $deps,
string or bool $ver,
bool $in_footer
);
wp_localize_script('my-script',
'my_script',
array( 'nonce' => wp_create_nonce( $this->nonce ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
法典中的注释:
必须在使用wp\u register\u script()
或wp\u enqueue\u script()注册脚本后调用wp\u localize\u script()
因此,您的工作流程应如下所示:
注册脚本
本地化
将本地化脚本排队
将其绑定到适当的操作:wp\u排队\u脚本
或admin\u排队\u脚本
例如:
将操作添加到您的\u构造中
方法:
add_action('wp_enqueue_scripts', array($this, 'registerAjaxScript'));
然后创建一个注册和本地化脚本的方法:
function registerAjaxScript() {
wp_register_script('my-script',
string $src,
array $deps,
string or bool $ver,
bool $in_footer
);
wp_localize_script('my-script',
'my_script',
array( 'nonce' => wp_create_nonce( $this->nonce ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
法典中的注释:
必须在使用wp\u register\u script()
或wp\u enqueue\u script()注册脚本后调用wp\u localize\u script()
因此,您的工作流程应如下所示:
注册脚本
本地化
将本地化脚本排队
将其绑定到适当的操作:wp\u排队\u脚本
或admin\u排队\u脚本
例如:
将操作添加到您的\u构造中
方法:
add_action('wp_enqueue_scripts', array($this, 'registerAjaxScript'));
然后创建一个注册和本地化脚本的方法:
function registerAjaxScript() {
wp_register_script('my-script',
string $src,
array $deps,
string or bool $ver,
bool $in_footer
);
wp_localize_script('my-script',
'my_script',
array( 'nonce' => wp_create_nonce( $this->nonce ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
脚本的本地化必须在包含脚本的页面上完成(即,在本例中,在nav-menus.php admin页面上)-您不包含实际将javascript排队的代码,但是您发布的代码向我表明,您实际上是在尝试在ajax调用本身中本地化脚本,这是行不通的
我在下面重新排列了您的代码,并添加了注释来解释每个更改:
class Myclass {
/**
* No reason not to assign this already (and I've renamed it to explicitly,
* let people reading the code know that it is not the nonce itself, but the
* name of the nonce action - fooled me for a minute or two :p
*/
private $nonce_action_name = 'my-plugin-nonce';
/**
* The __construct function is great for hooking everything you need to
* hook, and for setting initial variables. Pretty much anything else
* should NOT be in this function though!
*/
public function __construct(){
// Register your ajax action.
add_action( 'wp_ajax_run_action', array( $this, 'run' ) );
// Hook into the appropriate action for admin scripts
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}
public function scripts() {
/**
* I've negated your if-statement here - basically we don't want to do
* anything at all if we are not on the correct page - which is clearer
* this way - also you had it in the __construct function, which will
* actually produce a fatal error, since get_current_screen is not
* usually defined yet at that point(!)
*/
if( get_current_screen()->id !== 'nav-menus' ){
return;
}
//Now enqueue (or register) the script
wp_enqueue_script('my-script', plugins_url('/my-script.js', __FILE__));
//Then localize it
wp_localize_script(
'my-script',
'my_script',
array(
'nonce' => wp_create_nonce( $this->nonce_action_name ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
/**
* Actual ajax handler
*/
public function run(){
if( ! wp_verify_nonce( $_POST['nonce'], $this->nonce_action_name ) ) {
//This is a guess, but I think you'll want to wp_send_json_error
//here to match up with your success below
wp_send_json_error();
} else {
wp_send_json_success();
}
/**
* Always recommended to explicitly die() after handling ajax - though
* the wp_send_json_* family probably does it for you.
*/
die();
}
}
new Myclass;
最后一点需要注意的是,ajaxurl
实际上总是在admin中定义的,因此您实际上不需要将其添加到本地化中(尽管添加几个额外的字节会带来伤害)。脚本的本地化必须在包含脚本的页面上完成(在本例中,在nav-menus.php admin页面上)-您没有包含实际将javascript排队的代码,但您发布的代码向我表明,您实际上是在尝试在ajax调用本身中本地化脚本,这是行不通的
我在下面重新排列了您的代码,并添加了注释来解释每个更改:
class Myclass {
/**
* No reason not to assign this already (and I've renamed it to explicitly,
* let people reading the code know that it is not the nonce itself, but the
* name of the nonce action - fooled me for a minute or two :p
*/
private $nonce_action_name = 'my-plugin-nonce';
/**
* The __construct function is great for hooking everything you need to
* hook, and for setting initial variables. Pretty much anything else
* should NOT be in this function though!
*/
public function __construct(){
// Register your ajax action.
add_action( 'wp_ajax_run_action', array( $this, 'run' ) );
// Hook into the appropriate action for admin scripts
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}
public function scripts() {
/**
* I've negated your if-statement here - basically we don't want to do
* anything at all if we are not on the correct page - which is clearer
* this way - also you had it in the __construct function, which will
* actually produce a fatal error, since get_current_screen is not
* usually defined yet at that point(!)
*/
if( get_current_screen()->id !== 'nav-menus' ){
return;
}
//Now enqueue (or register) the script
wp_enqueue_script('my-script', plugins_url('/my-script.js', __FILE__));
//Then localize it
wp_localize_script(
'my-script',
'my_script',
array(
'nonce' => wp_create_nonce( $this->nonce_action_name ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
/**
* Actual ajax handler
*/
public function run(){
if( ! wp_verify_nonce( $_POST['nonce'], $this->nonce_action_name ) ) {
//This is a guess, but I think you'll want to wp_send_json_error
//here to match up with your success below
wp_send_json_error();
} else {
wp_send_json_success();
}
/**
* Always recommended to explicitly die() after handling ajax - though
* the wp_send_json_* family probably does it for you.
*/
die();
}
}
new Myclass;
最后一点需要注意的是,ajaxurl
实际上总是在admin中定义的,因此您实际上不需要将其添加到本地化中(尽管添加几个额外的字节会带来伤害)。脚本的本地化必须在包含脚本的页面上完成(在本例中,在nav-menus.php admin页面上)-您没有包含实际将javascript排队的代码,但您发布的代码向我表明,您实际上是在尝试在ajax调用本身中本地化脚本,这是行不通的
我在下面重新排列了您的代码,并添加了注释来解释每个更改:
class Myclass {
/**
* No reason not to assign this already (and I've renamed it to explicitly,
* let people reading the code know that it is not the nonce itself, but the
* name of the nonce action - fooled me for a minute or two :p
*/
private $nonce_action_name = 'my-plugin-nonce';
/**
* The __construct function is great for hooking everything you need to
* hook, and for setting initial variables. Pretty much anything else
* should NOT be in this function though!
*/
public function __construct(){
// Register your ajax action.
add_action( 'wp_ajax_run_action', array( $this, 'run' ) );
// Hook into the appropriate action for admin scripts
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}
public function scripts() {
/**
* I've negated your if-statement here - basically we don't want to do
* anything at all if we are not on the correct page - which is clearer
* this way - also you had it in the __construct function, which will
* actually produce a fatal error, since get_current_screen is not
* usually defined yet at that point(!)
*/
if( get_current_screen()->id !== 'nav-menus' ){
return;
}
//Now enqueue (or register) the script
wp_enqueue_script('my-script', plugins_url('/my-script.js', __FILE__));
//Then localize it
wp_localize_script(
'my-script',
'my_script',
array(
'nonce' => wp_create_nonce( $this->nonce_action_name ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
/**
* Actual ajax handler
*/
public function run(){
if( ! wp_verify_nonce( $_POST['nonce'], $this->nonce_action_name ) ) {
//This is a guess, but I think you'll want to wp_send_json_error
//here to match up with your success below
wp_send_json_error();
} else {
wp_send_json_success();
}
/**
* Always recommended to explicitly die() after handling ajax - though
* the wp_send_json_* family probably does it for you.
*/
die();
}
}
new Myclass;
最后一点需要注意的是,ajaxurl
实际上总是在admin中定义的,因此您实际上不需要将其添加到本地化中(尽管添加几个额外的字节会带来伤害)。脚本的本地化必须在包含脚本的页面上完成(在本例中,在nav-menus.php admin页面上)-您没有包含实际将javascript排队的代码,但您发布的代码向我表明,您实际上是在尝试在ajax调用本身中本地化脚本,这是行不通的
我在下面重新排列了您的代码,并添加了注释来解释每个更改:
class Myclass {
/**
* No reason not to assign this already (and I've renamed it to explicitly,
* let people reading the code know that it is not the nonce itself, but the
* name of the nonce action - fooled me for a minute or two :p
*/
private $nonce_action_name = 'my-plugin-nonce';
/**
* The __construct function is great for hooking everything you need to
* hook, and for setting initial variables. Pretty much anything else
* should NOT be in this function though!
*/
public function __construct(){
// Register your ajax action.
add_action( 'wp_ajax_run_action', array( $this, 'run' ) );
// Hook into the appropriate action for admin scripts
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}
public function scripts() {
/**
* I've negated your if-statement here - basically we don't want to do
* anything at all if we are not on the correct page - which is clearer
* this way - also you had it in the __construct function, which will
* actually produce a fatal error, since get_current_screen is not
* usually defined yet at that point(!)
*/
if( get_current_screen()->id !== 'nav-menus' ){
return;
}
//Now enqueue (or register) the script
wp_enqueue_script('my-script', plugins_url('/my-script.js', __FILE__));
//Then localize it
wp_localize_script(
'my-script',
'my_script',
array(
'nonce' => wp_create_nonce( $this->nonce_action_name ),
'ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
/**
* Actual ajax handler
*/
public function run(){
if( ! wp_verify_nonce( $_POST['nonce'], $this->nonce_action_name ) ) {
//This is a guess, but I think you'll want to wp_send_json_error
//here to match up with your success below
wp_send_json_error();
} else {
wp_send_json_success();
}
/**
* Always recommended to explicitly die() after handling ajax - though
* the wp_send_json_* family probably does it for you.
*/
die();
}
}
new Myclass;
最后一点需要注意的是,ajaxurl
实际上总是在admin中定义的,因此您实际上不需要将其添加到本地化中(尽管添加几个额外的字节会带来伤害)。您的变量被本地化为my_script
,但您在javascript中使用的是my_Data
,这是故意的吗?如果你在Chrome中运行它,并且开发工具中的网络选项卡打开,那么参数和返回值看起来正确吗?很好,Hobo,我更新了这个问题,但它仍然没有做它应该做的事情。你的变量被本地化为my_script
,但是你在javascript中使用my_Data
,这是故意的吗?如果你在Chrome中运行它,并且开发工具中的“网络”选项卡处于打开状态,那么参数和返回值看起来正确吗