I know this has been asked 1000 times already, but I'm not seeing anything wrong in my code and am still getting a 400 error (XHR).
Here is the code from my plugin:
require_once( plugin_dir_path( __FILE__ ) . 'classes/duplicate_statement_controller.class.php' );
//add_action( 'plugins_loaded', array( 'UWSA_Calendar_Controller', 'get_instance' ) );
add_action( 'wp_enqueue_scripts', function(){
wp_enqueue_style('duplicate-tax-css' , plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.css' );
wp_enqueue_script( 'duplicate-tax-js', plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.js', array('jquery'), '1.0', true );
wp_localize_script( 'duplicate-tax-js', 'tax_object',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce('tax-nonce')
)
);
} );
My jQuery AJAX call:
$.ajax({
url: tax_object.ajax_url,
type: 'post',
data: {
action: 'add_row',
nonce: tax_object.nonce
},
success: function (response) {
// probably re-calc here
console.log(response);
}
});
And the code I want to run:
private function __construct() {
//require_once( plugin_dir_path( __FILE__ ) . 'duplicate-tax-manage-rows.class.php' );
//require_once( plugin_dir_path( __FILE__ ) . 'duplicate-tax-calc.class.php' );
//Wordpress hooks and filters
add_action( 'init', array ( $this , 'action_init' ) );
// Ajax actions for rows
add_action('wp_ajax_add_row', array( $this , 'add_row' ) );
add_action('wp_ajax_nopriv_add_row', array( $this , 'add_row' ) );
add_action('wp_ajax_remove_row', array( $this , 'remove_row' ) );
add_action('wp_ajax_nopriv_remove_row', array( $this , 'remove_row' ) );
}
public function add_row() {
error_log("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Every time I run this, I get a 400 error (strict-origin-when-cross-origin).
I just don't see anything wrong. Does anyone see something I'm not?
To clarify some, I have registered the AJAX calls, put them in wp_enqueue and wp_localize_script. Also make calls with wp_ajax_nopriv as suggested elsewhere. As far as I can tell, all the syntax is correct.
This code is even used in another plugin without error so I'm not sure if this is a problem with the code or a setting somewhere else.
Please, I really need help.