简体   繁体   中英

Wordpress AJAX request on admin-ajax.php gives error 400

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:

File /wp-content/plugins/prova/prova.php:

<?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["param"];
  echo $param;
  wp_die();
}

/**
 * Register the "book" 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("wp_ajax_nopriv_mia_azione", "risposta_mia_azione");

?>

And the root of my Wordpress installation is http://123.compute.amazonaws.com/wordpress/ . 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 POST http://132.compute.amazonaws.com/wordpress/wp-admin/admin-ajax.php 400 (Bad Request) :

(function( $ ) {
$.ajax({
    url : 'http://123.compute.amazonaws.com/wordpress/wp-admin/admin-ajax.php',
    data : {"action" : "mia_azione", "param" : "ciao"},
    method : 'POST',
    success : function( response ){ console.log(response) },
    error : function(error){ console.log(error) }
  })
})(jQuery)

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:

ajaxresponse = await fetch("http://ec2-3-129-17-101.us-east-2.compute.amazonaws.com/wordpress/wp-admin/admin-ajax.php", {
    method: 'POST', mode: 'no-cors',
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: JSON.stringify({action: "mia_azione", param: "ciao"}) 
  });

will give me a response like:

body: ReadableStream
bodyUsed: true
headers: Headers {}
ok: false
redirected: false
status: 400
statusText: "Bad Request"
type: "basic"
url: "http://123.compute.amazonaws.com/wordpress/w

May anyone help me?

EDIT : as pointed out by the response, I was using the wrong functionality - what I needed was this: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

Maybe you need something like this:

function my_enqueue() {
      wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
      wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
 }
 add_action( 'wp_enqueue_scripts', 'my_enqueue' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM