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')    

I have then got a function and using add_action to register it

 add_action( 'wp_ajax_nopriv_processingHook','processingHook');

 function processingHook() {
     echo "111";

And then in the jQuery

   type: "POST",
   dataType: "json",
   url: frontend_ajax.ajax_url,
   data: {
        action: 'processingHook',
        newForm: formstuff,
        security_nonce: nonce

    success: function (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 ?


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 ?

  • try changing nonce value like security_nonce : frontend_ajax.nonce – Chetan Vaghela Oct 16 at 8:41
  • Hi Chetan, I have tried that. the nonce in the code above is a var i've assigned the nonce in jquery. also tried frontend_ajax.nonce, frontend_ajax.security_nonce. – David Oct 16 at 9:36
  • where did you define actions for wp_ajax ? in which file? – Chetan Vaghela Oct 16 at 9:38
  • The processingHook function that is called is defined in the same php file as wp_localize_script and add_action(wp_ajax_ – David Oct 16 at 11:08
  • okay so as I said above I have another ajax function that is working fine and running from another php file for the backend/admin side. And for kicks I just dropped the processingHook function and the add_action('wp_ajax_ calls into that php file. The wp_enqueue and wp_localize_script are still in the frontend php file and it works. – David Oct 16 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.