title: Wordpress AJAX request on admin-ajax.php gives error 400
body: <p>I'm trying to create a Wordpress plugin that will receive a simple AJAX request and, for the moment, just post its body. My code is:</p>
  <p><strong>File /wp-content/plugins/prova/prova.php:</strong></p>
  <pre><code>&lt;?php
  
  /**
   * Plugin Name:       My Basics Plugin
   * Plugin URI:        https://example.com/plugins/the-basics/
   * Description:       Handle the basics with this plugin.
   */
  
  function risposta_mia_azione() {
    $param = $_POST[&quot;param&quot;];
    echo $param;
    wp_die();
  }
  
  /**
   * Register the &quot;book&quot; custom post type
   */
  function pluginprefix_setup_post_type() {
  } 
  add_action( 'init', 'pluginprefix_setup_post_type' );
  
  /**
  * Activate the plugin.
  */
  function pluginprefix_activate() { 
  }
  register_activation_hook( __FILE__, 'pluginprefix_activate' );
  
  /**
   * Deactivation hook.
   */
  function pluginprefix_deactivate() {
  }
  register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );
  
  /**
   * Uninstallation hook.
   */
  function pluginprefix_uninstall() {
  
  }
  register_uninstall_hook( __FILE__, 'pluginprefix_uninstall' );
  
  
  add_action(&quot;wp_ajax_nopriv_mia_azione&quot;, &quot;risposta_mia_azione&quot;);
  
  ?&gt;
  
  </code></pre>
  <p>And the root of my Wordpress installation is <code>http://123.compute.amazonaws.com/wordpress/</code>. Now, if I open the dev tools console inside a page of that installation and paste the following jQuery ajax call I will get a <code>POST http://132.compute.amazonaws.com/wordpress/wp-admin/admin-ajax.php 400 (Bad Request)</code>:</p>
  <pre><code>(function( $ ) {
  $.ajax({
      url : 'http://123.compute.amazonaws.com/wordpress/wp-admin/admin-ajax.php',
      data : {&quot;action&quot; : &quot;mia_azione&quot;, &quot;param&quot; : &quot;ciao&quot;},
      method : 'POST',
      success : function( response ){ console.log(response) },
      error : function(error){ console.log(error) }
    })
  })(jQuery)
  </code></pre>
  <p>I haven't touched Wordpress in years and I'm quite unfamiliar with the under-the-hood of Wordpress, from what I read in similar questions this is usually the case when the request in incorrectly formed, but I can't really see my error. For instance, a call like:</p>
  <pre><code>ajaxresponse = await fetch(&quot;http://ec2-3-129-17-101.us-east-2.compute.amazonaws.com/wordpress/wp-admin/admin-ajax.php&quot;, {
      method: 'POST', mode: 'no-cors',
      headers: {
        'Content-Type': 'application/json'
        // 'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: JSON.stringify({action: &quot;mia_azione&quot;, param: &quot;ciao&quot;}) 
    });
  </code></pre>
  <p>will give me a response like:</p>
  <pre><code>body: ReadableStream
  bodyUsed: true
  headers: Headers {}
  ok: false
  redirected: false
  status: 400
  statusText: &quot;Bad Request&quot;
  type: &quot;basic&quot;
  url: &quot;http://123.compute.amazonaws.com/wordpress/w
  </code></pre>
  <p>May anyone help me?</p>
  <p><strong>EDIT</strong>: as pointed out by the response, I was using the wrong functionality - what I needed was this: <a href="https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/" rel="nofollow noreferrer">https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/</a></p>