title: Wordpress: Why am I getting a 400 from ajaxurl?
body: <p>I'm trying to implement basic ajax. I've been following <a href="https://pippinsplugins.com/using-ajax-your-plugin-wordpress-admin/" rel="nofollow noreferrer">this tutorial</a>. My JS fires when I click the button but I get a <code>400</code> from the ajax url: <code>POST http://localhost:8080/wp-admin/admin-ajax.php 400 (Bad Request)</code></p>
  
  <p>As far as I can tell, I haven't strayed far from the tutorial code. Her is my JS:</p>
  
  <pre><code>jQuery(document).ready(function($){
      $('#baa_form').submit(function(){
  
          var data = {
              action: 'baa_response'
          } 
  
          $.post(ajaxurl, data, function(response){
              alert('Hello');
          });
  
          return false;
      });
  });
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code> &lt;?php
  function baa_add_menu() {
      global $baa_settings_page;
  
      $baa_settings_page = add_menu_page( 'Basic Admin Ajax', 'Basic Admin Ajax', 'edit_pages', 'basic-admin-ajax', 'baa_render_settings_page', false, 62.1 );
  }
  
  function baa_load_scripts( $hook ) {
      global $baa_settings_page;
      if ( $hook !== $baa_settings_page ) {
          return;
      }
      $path = plugin_dir_url( __FILE__ ) . 'js/basic-admin-ajax.js';
      wp_enqueue_script( 'basic-admin-ajax', $path, array( 'jquery' ) );
  }
  
  add_action( 'admin_menu', 'baa_add_menu' );
  add_action( 'admin_enqueue_scripts', 'baa_load_scripts' );
  
  function baa_render_settings_page() {
      ?&gt;
      &lt;form id="baa_form" method="POST"&gt;
          &lt;div&gt;
              &lt;input type="submit" name="baa_submit_button" id="baa_submit_button" class="button-primary" value="go ajax"&gt;
          &lt;/div&gt;
      &lt;/form&gt;
      &lt;?php
  }
  
  function baa_response() {
      die( 'I got died' );
  }
  </code></pre>
  
  <p>I've had a look inside <code>admin-ajax.php</code> and the only reason for returning a <code>400</code> that I can see is if I've failed to set an action. <code>data['action']</code> is certainly set, unless I'm crazy.</p>
  
  <p>Am I doing anything that's obviously wrong? What could be causing the <code>400</code> response?</p>
  
  <p><strong>update</strong>
  To clarify, the JS fires but the request to <code>admin-ajax.php</code> receives a <code>400</code>. You can see where the tutorial make has implemented similar JS <a href="https://www.youtube.com/watch?time_continue=1799&amp;v=7pO-FYVZv94" rel="nofollow noreferrer">here</a>. (I haven't added a nonce yet but the video maker had already demonstrated it working without implementing a nonce.)</p>
  
  <p><strong>edit</strong>
  I've updated the PHP to show the plugin's entire PHP file.</p>
  
  <p><strong>update 2</strong>
  I've stripped the JS back to the essentials and followed the top example from <a href="https://codex.wordpress.org/AJAX_in_Plugins" rel="nofollow noreferrer">Wordpress</a>. The response is still <code>400</code>. Which leads me to believe that it might be something I've overlooked in the php. At the moment, I don't understand why or what it might be.</p>
  
  <pre><code>jQuery(document).ready(function($) {
      var data = {
          'action': 'baa_response'
      };
  
      $.post(ajaxurl, data, function(response) {
          alert('Got this from the server: ' + response);
      });
  });
  </code></pre>