Not sure why this error occur! you may follow this article Or you can review your code in following steps:
Use wp_enqueue_scripts
and wp_ajax_{action_name}
wp_ajax_nopriv_{action_name}
in your plugin file. Suppose my plugin name is test-plugin
and my main plugin file name is test-plugin.php
<?php
/*
Plugin Name: Test Plugin
Plugin URI: http://wordpress.org
Description: This just a test plugin.
Author: Razon
Version: 1.0.0
Author URI: http://raazon.github.io/
*/
add_action('wp_enqueue_scripts', 'wp_docs_enqueue_scripts');
function wp_docs_enqueue_scripts()
{
wp_enqueue_script('scripts', plugin_dir_url(__FILE__) . 'scripts.js', ['jquery']);
wp_localize_script('scripts', 'ajax_object', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'lstJugadoresParticipantesPlaceholder' => __('Find Players', 'leaguemanagement'),
'lstEquiposTeamsPlaceholder' => __('Find Teams', 'leaguemanagement'),
));
}
add_action('wp_ajax_actualizar_jornada', 'actualizar_jornada');
add_action('wp_ajax_nopriv_actualizar_jornada', 'actualizar_jornada');
function actualizar_jornada()
{
$id = isset($_POST['id']) ? $_POST['id'] : '';
echo $id;
wp_die();
}
And my JS
file scripts.js
(function ($) {
"use strict";
jQuery(document).ready(function () {
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
data: {
action: 'actualizar_jornada',
id: 100,
},
beforeSend: function () {
console.log('Sending request...');
},
success: function (response) {
console.log(response);
},
error: function (errorThrown, status, error) {
console.log(status);
}
});
});
})(jQuery);
I have tested this in my local and work properly. If this code not work in your end then there are other issue in in your website / host