'admin-ajax 400 Bad Request
Hi I have a plugin class which I am modifying.
I am trying to create an ajax call to call a function from php on button click. This is my code:
function import() {
$this->get_file();
echo ("<h1>Sample of the first 3 records in file</h1><br>");
$this->get_sample_of_records();
echo("<button type=\"button\" onclick=\"import_file()\"> Import </button>");
}
In Javascript this is my jquery:
function import_file () {
jQuery.ajax({
url: ajaxurl,
data: {'action': 'post_final_save'},
method : 'POST', //Post method
success:function(result){
alert(result);
}
});
}
This is the function I want to call:
public function final_save(){
echo 'here';
die();
}
And this is where the class is initialized and the action hook called:
function really_simple_csv_importer() {
load_plugin_textdomain( 'really-simple-csv-importer', false, dirname( plugin_basename(__FILE__) ) . '/languages' );
$rs_csv_importer = new RS_CSV_Importer();
add_action( 'wp_ajax_post_final_save', array( $rs_csv_importer, 'final_save' ) );
add_action( 'wp_ajax_nopriv_post_final_save', array( $rs_csv_importer, 'final_save' ) );
register_importer('csv', __('CSV', 'really-simple-csv-importer'), __('Import posts, categories, tags, custom fields from simple csv file.', 'really-simple-csv-importer'), array ($rs_csv_importer, 'dispatch'));
}
add_action( 'plugins_loaded', 'really_simple_csv_importer' );
This is the dispatch function called by register_importer:
function dispatch() {
$this->header();
$this->enqueue_script();
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
check_admin_referer('import-upload');
set_time_limit(0);
$this->import();
break;
}
$this->footer();
}
I am getting the following error:
/wp-admin/admin-ajax.php 400 (Bad Request)
Any idea why and how I can fix it please?
To add a little more context - This class is extending WP_Importer
if ( !defined('WP_LOAD_IMPORTERS') )
return;
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( !class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) )
require_once $class_wp_importer;
}
// Load Helpers
require dirname( __FILE__ ) . '/class-rs_csv_helper.php';
require dirname( __FILE__ ) . '/class-rscsv_import_post_helper.php';
/**
* CSV Importer
*
* @package WordPress
* @subpackage Importer
*/
if ( class_exists( 'WP_Importer' ) ) {
class RS_CSV_Importer extends WP_Importer {
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
