Endpoint not found, but correctly registered
-
I am developing a plugin, and on an admin page I have a table listing the entries from a custom mysql table. I added a checkbox for each row containing the id to enable the deletion of the rows checked.
I have problems in sending data to the endpoint, as it is not found (404 error). I used the fetch method for the first time (in another front-end page of the plugin I use endpoints but not with fetch and they work fine), as I found it here https://stackoverflow.com/questions/46204166/wordpress-rest-api-authentication-using-fetchHere the function for the axax call on the admin page
<script> //check all checkboxes (function( $ ) { // Function to handle AJAX requests for deleting and exporting entries function handleAjax(action, data) { // Construct the URL for the REST API endpoint var customEndpointUrl = '/wp-json/sight-form-tethys/v1/' + action; // Make the AJAX request using fetch fetch(customEndpointUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>' }, credentials: "same-origin", body: JSON.stringify({ data: data }) }) .then(response => { if (response.ok) { // Handle success response console.log('Entries ' + action + ' successfully'); } else { // Handle error response console.error('Error ' + action + ' entries'); } }) .catch(error => { console.error('Error:', error); }); } $('#delete_selected').on('click', function() { var selectedEntries = []; $('input[type="checkbox"]:checked').each(function() { var entryId = $(this).attr('name'); selectedEntries.push(entryId); }); console.log(selectedEntries); handleAjax('delete_entries_ajax', selectedEntries); }); })( jQuery ); </script>
and this is the registered endpoint
public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; add_action('admin_menu', array( $this, 'addPluginAdminMenu' ), 9); add_action('admin_init', array( $this, 'registerAndBuildFields' )); add_action('admin_notices', array( $this,'sigh_form_tethys_admin_notice')); add_action('rest_api_init', array( $this, 'register_delete_entries_endpoint')); } public function register_delete_entries_endpoint() { $namespace = 'sight-form-tethys/v1'; $route = 'delete_entries_ajax'; register_rest_route($namespace, $route, array( 'methods' => 'POST', 'callback' => [$this,'delete_entries_rest_api_callback'], 'permission_callback' => [$this, 'delete_entries_rest_api_permissions_check'], )); } public function delete_entries_rest_api_permissions_check($request) { // Perform permission check logic here return current_user_can('manage_options'); } public function delete_entries_rest_api_callback($request) { $selectedEntries = $request->get_param('data'); file_put_contents('somefilename.txt', print_r($selectedEntries, true), FILE_APPEND); // Perform delete operation for selected entries // Example: $wpdb->delete($table_name, ['id' => $selectedEntries]); return new WP_REST_Response(array('message' => 'Entries deleted successfully'), 200); }
I noticed that after few seconds the error 404 is reported inspecting the Network, I see a series of ajax.php POST containing the id sent (the first, if more than one row is selected) with the following request, like if not finding the endpoint he call is sent to ajax.php
Interval "60" _nonce "b50b096d6e" action "heartbeat" screen_id "sighting-form-tethys_page_sighting-form-tethys-entries" has_focus "false"
I already console.log all variables in the ajax call and they are correct. I also checked in wp-json that the endpoint created is registered.
What is the problem? Thnaks a lot
- The topic ‘Endpoint not found, but correctly registered’ is closed to new replies.