admin管理员组文章数量:1122832
I'm making a plugin with a class and methods.
This class gets called through the theme ( hard coded ) to enqueue and localize my js. it also prepares it for ajax. However, i keep getting ERROR: 400
POST
/wp-admin/admin-ajax.php
400 (Bad Request)
I've ran ajax outside of a class in multiple plugins and at the functions.php with no issues, but i can't get it to work inside a class.
This is my code:
class CCYTFeatured {
public function __construct(){
$this->cc_yt_scripts();
add_action( 'wp_enqueue_scripts', array($this, 'cc_yt_scripts' ));
add_action( 'wp_ajax_cc_yt_featured', array( $this, 'cc_yt_featured' ) );
add_action( 'wp_ajax_nopriv_cc_yt_featured', array( $this, 'cc_yt_featured' ) );
}
// Load CSS AND JAVA FRONT END
public function cc_yt_scripts() {
wp_register_style( 'cc_yt_style',
plugins_url( '/css/cc_yt.css', __FILE__ ),
array(),
cc_yt_version()
);
wp_enqueue_style('cc_yt_style');
// JAVASCRIPT
wp_register_script( 'cc_yt_javascript',
plugins_url( '/js/cc_yt.js', __FILE__ ),
array('jquery'),
cc_yt_version(),
true
);
wp_enqueue_script('cc_yt_javascript');
wp_localize_script(
'cc_yt_javascript',
'cc_yt_script',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' , __FILE__ ),
'ajax_nonce' => wp_create_nonce('my_nonce'),
)
);
}
public function cc_yt_featured(){
check_ajax_referer( 'my_nonce', 'security' );
echo json_encode('IM HERE, YOU CAN FIND US');
}
}
This is my JavaScript ajax call:
jQuery.ajax({
url: cc_yt_script.ajaxurl,
type : 'POST',
data: {
action : 'cc_yt_featured',
security: cc_yt_script.ajax_nonce,
post: 'bananas',
},
success: function(response) {
console.log('THIS IS THE RESPONSE: '+ response);
}
});
I keep getting the same error, i can't figure out what is wrong with my class:
This is how i call it.
$cc_yt = new CCYTFeatured;
I have that call hard coded inside a theme file. This does enqueue the styles and scripts successfully.
The ajax get is the following:
/wp-admin/admin-ajax.php?action=cc_yt_featured&security=79e39910b8&post=bananas
Anyone have any clue what i'm doing wrong??
Thanks!
I'm making a plugin with a class and methods.
This class gets called through the theme ( hard coded ) to enqueue and localize my js. it also prepares it for ajax. However, i keep getting ERROR: 400
POST
http://www.devsite.local/wp-admin/admin-ajax.php
400 (Bad Request)
I've ran ajax outside of a class in multiple plugins and at the functions.php with no issues, but i can't get it to work inside a class.
This is my code:
class CCYTFeatured {
public function __construct(){
$this->cc_yt_scripts();
add_action( 'wp_enqueue_scripts', array($this, 'cc_yt_scripts' ));
add_action( 'wp_ajax_cc_yt_featured', array( $this, 'cc_yt_featured' ) );
add_action( 'wp_ajax_nopriv_cc_yt_featured', array( $this, 'cc_yt_featured' ) );
}
// Load CSS AND JAVA FRONT END
public function cc_yt_scripts() {
wp_register_style( 'cc_yt_style',
plugins_url( '/css/cc_yt.css', __FILE__ ),
array(),
cc_yt_version()
);
wp_enqueue_style('cc_yt_style');
// JAVASCRIPT
wp_register_script( 'cc_yt_javascript',
plugins_url( '/js/cc_yt.js', __FILE__ ),
array('jquery'),
cc_yt_version(),
true
);
wp_enqueue_script('cc_yt_javascript');
wp_localize_script(
'cc_yt_javascript',
'cc_yt_script',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' , __FILE__ ),
'ajax_nonce' => wp_create_nonce('my_nonce'),
)
);
}
public function cc_yt_featured(){
check_ajax_referer( 'my_nonce', 'security' );
echo json_encode('IM HERE, YOU CAN FIND US');
}
}
This is my JavaScript ajax call:
jQuery.ajax({
url: cc_yt_script.ajaxurl,
type : 'POST',
data: {
action : 'cc_yt_featured',
security: cc_yt_script.ajax_nonce,
post: 'bananas',
},
success: function(response) {
console.log('THIS IS THE RESPONSE: '+ response);
}
});
I keep getting the same error, i can't figure out what is wrong with my class:
This is how i call it.
$cc_yt = new CCYTFeatured;
I have that call hard coded inside a theme file. This does enqueue the styles and scripts successfully.
The ajax get is the following:
http://www.devsite.local/wp-admin/admin-ajax.php?action=cc_yt_featured&security=79e39910b8&post=bananas
Anyone have any clue what i'm doing wrong??
Thanks!
Share Improve this question edited Nov 12, 2018 at 23:15 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Nov 12, 2018 at 23:12 Jhonatan VillenaJhonatan Villena 1012 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 0All the code is correct except the way i was calling.
I created a different method for __construct and then called it on the plugin it self so the scripts and styles get hooked before the templates. In my template i called the class again and the methods i need it. I no longer constructed my method all at once.
本文标签: javascriptAjax call from Plugin using Class
版权声明:本文标题:javascript - Ajax call from Plugin using Class 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289376a1928292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$cc_yt = new CCYTFeatured;
in your functions.php file? – czerspalace Commented Nov 12, 2018 at 23:29$cc_yt = new CCYTFeatured;
is inside a template, then it's not going to be run during an AJAX request, which means your callbacks won't be hooked, leading to a 400 error. – Jacob Peattie Commented Nov 13, 2018 at 0:19