admin管理员组文章数量:1130349
I´m trying to do a plugin feature : download a file by a button click using wp_ajax feature.
I already used wp_ajax but in this case I did not find my error.
When I click on the button, it runs correctly the js file, but return the html page instead to call getPrivateFileByAjax().
JS
jQuery( function($) {
$(document).ready( function() {
$(".download-file").on( "click", function( event ) {
var file_name = $(this).attr("data-file");
var data = {
'action': "getPrivateFileByAjax",
'file_name' : file_name
};
$.post( wgs_ajax_object.ajaxurl, data, function( response ) {
console.dir( response );
});
});
});
});
My Shortcode page
class FileDownloadsShortcode extends Shortcode {
public function check_page() {
global $post;
if( !empty( $post ) && has_shortcode( $post->post_content, $this->tag ) ){
add_action('wp_enqueue_scripts', array( $this , 'set_scripts'));
}
}
public function set_scripts() {
//Javascripts
wp_enqueue_script( "downloadsscript", "path-to-my-js.js" );
wp_localize_script( 'downloadsscript', 'wgs_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
);
}
}
Admin init file
class Admin extends FooPlugin {
public function __construct() {
parent::__construct();
add_action( "wp_ajax_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
add_action( "wp_ajax_nopriv_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
}
public static function getPrivateFileByAjax(){
echo "test";
wp_die();
}
}
I´m trying to do a plugin feature : download a file by a button click using wp_ajax feature.
I already used wp_ajax but in this case I did not find my error.
When I click on the button, it runs correctly the js file, but return the html page instead to call getPrivateFileByAjax().
JS
jQuery( function($) {
$(document).ready( function() {
$(".download-file").on( "click", function( event ) {
var file_name = $(this).attr("data-file");
var data = {
'action': "getPrivateFileByAjax",
'file_name' : file_name
};
$.post( wgs_ajax_object.ajaxurl, data, function( response ) {
console.dir( response );
});
});
});
});
My Shortcode page
class FileDownloadsShortcode extends Shortcode {
public function check_page() {
global $post;
if( !empty( $post ) && has_shortcode( $post->post_content, $this->tag ) ){
add_action('wp_enqueue_scripts', array( $this , 'set_scripts'));
}
}
public function set_scripts() {
//Javascripts
wp_enqueue_script( "downloadsscript", "path-to-my-js.js" );
wp_localize_script( 'downloadsscript', 'wgs_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
);
}
}
Admin init file
class Admin extends FooPlugin {
public function __construct() {
parent::__construct();
add_action( "wp_ajax_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
add_action( "wp_ajax_nopriv_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
}
public static function getPrivateFileByAjax(){
echo "test";
wp_die();
}
}
Share
Improve this question
edited Nov 29, 2018 at 12:49
J.BizMai
asked Nov 29, 2018 at 12:42
J.BizMaiJ.BizMai
9302 gold badges10 silver badges30 bronze badges
1 Answer
Reset to default 0My error was in my JS file.
I had to write
wgs_ajax_object.ajax_urlinstead ofwgs_ajax_object.ajaxurl
I´m trying to do a plugin feature : download a file by a button click using wp_ajax feature.
I already used wp_ajax but in this case I did not find my error.
When I click on the button, it runs correctly the js file, but return the html page instead to call getPrivateFileByAjax().
JS
jQuery( function($) {
$(document).ready( function() {
$(".download-file").on( "click", function( event ) {
var file_name = $(this).attr("data-file");
var data = {
'action': "getPrivateFileByAjax",
'file_name' : file_name
};
$.post( wgs_ajax_object.ajaxurl, data, function( response ) {
console.dir( response );
});
});
});
});
My Shortcode page
class FileDownloadsShortcode extends Shortcode {
public function check_page() {
global $post;
if( !empty( $post ) && has_shortcode( $post->post_content, $this->tag ) ){
add_action('wp_enqueue_scripts', array( $this , 'set_scripts'));
}
}
public function set_scripts() {
//Javascripts
wp_enqueue_script( "downloadsscript", "path-to-my-js.js" );
wp_localize_script( 'downloadsscript', 'wgs_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
);
}
}
Admin init file
class Admin extends FooPlugin {
public function __construct() {
parent::__construct();
add_action( "wp_ajax_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
add_action( "wp_ajax_nopriv_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
}
public static function getPrivateFileByAjax(){
echo "test";
wp_die();
}
}
I´m trying to do a plugin feature : download a file by a button click using wp_ajax feature.
I already used wp_ajax but in this case I did not find my error.
When I click on the button, it runs correctly the js file, but return the html page instead to call getPrivateFileByAjax().
JS
jQuery( function($) {
$(document).ready( function() {
$(".download-file").on( "click", function( event ) {
var file_name = $(this).attr("data-file");
var data = {
'action': "getPrivateFileByAjax",
'file_name' : file_name
};
$.post( wgs_ajax_object.ajaxurl, data, function( response ) {
console.dir( response );
});
});
});
});
My Shortcode page
class FileDownloadsShortcode extends Shortcode {
public function check_page() {
global $post;
if( !empty( $post ) && has_shortcode( $post->post_content, $this->tag ) ){
add_action('wp_enqueue_scripts', array( $this , 'set_scripts'));
}
}
public function set_scripts() {
//Javascripts
wp_enqueue_script( "downloadsscript", "path-to-my-js.js" );
wp_localize_script( 'downloadsscript', 'wgs_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
);
}
}
Admin init file
class Admin extends FooPlugin {
public function __construct() {
parent::__construct();
add_action( "wp_ajax_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
add_action( "wp_ajax_nopriv_getPrivateFileByAjax", array( $this, "getPrivateFileByAjax" ) );
}
public static function getPrivateFileByAjax(){
echo "test";
wp_die();
}
}
Share
Improve this question
edited Nov 29, 2018 at 12:49
J.BizMai
asked Nov 29, 2018 at 12:42
J.BizMaiJ.BizMai
9302 gold badges10 silver badges30 bronze badges
1 Answer
Reset to default 0My error was in my JS file.
I had to write
wgs_ajax_object.ajax_urlinstead ofwgs_ajax_object.ajaxurl
本文标签: plugin developmentwpajax function return the html page
版权声明:本文标题:plugin development - wp_ajax function return the html page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749141744a2322308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论