Trying to run a ajax request back to a function and keep getting a 400 error. this is the code below
wp_register_script('frontend_js', plugins_url('/js/frontend.js', __FILE__), array(), false, true);
wp_localize_script('frontend_js', 'frontend_ajax', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce('stuff here')
));
wp_enqueue_script('frontend_js');
I have then got a function and using add_action to register it
add_action('wp_ajax_processingHook','processingHook');
add_action( 'wp_ajax_nopriv_processingHook','processingHook');
function processingHook() {
echo "111";
}
And then in the jQuery
jQuery.ajax({
type: "POST",
dataType: "json",
url: frontend_ajax.ajax_url,
data: {
action: 'processingHook',
newForm: formstuff,
security_nonce: nonce
},
success: function (msg) {
alert(msg);
// Do something when done
},
error: function (xhr, status, error) {
var err = JSON.parse(xhr.responseText);
console.log('err = ' + err);
console.log('status = ' + status);
console.log('error = ' + error);
}
});
And I get a 400 error.
I have a very similar ajax working in the admin pages successfully. Using console.log I get the correct ajax url and when I browse to the admin-ajax.php url I get a 0 on the page.
Am I missing something running ajax in the frontend of of a wordpress site ? is there a permission or something that needs to be set to send data to via ajax ? Or is running 2 separate ajax calls like this, backend front end, problematic ?
Cheers
EDIT: - Some clarification there are two files admin and front end.
Admin file contains an ajax function that works. Frontend file contains the above function, processHook()
As I said below in comments I moved the processHook() function and add_action(wp_ajax into the admin file and IT WORKED. I left the wp_localize call in the frontend file.
Can anyone explain this ? Does it have anything to do with the way and timing of the files being loaded ? Is there meant to be a priority to the add_action(wp_ajax_ calls ?