#108169Ajaxrouter for Backend not working
Description
Hi,
For a backend module I set up an Ajaxrouter in “Configuration/Backend/AjaxRouter.php”.
I also created a controller according to this dosumentation:
https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Backend/Ajax.html
use Mikelmade\Dahmsfunc\Controller\AjaxController;
return [
'dahmsfunc_ajax_upload' => [
'path' => '/dahmsfunc/ajaxupload',
'methods' => ['POST'],
'target' => AjaxController::class . '::uploadAction',
],
];
In a custom Javascript I am using I am trying to make the appropriate Ajax call (with jQuery):
$.ajax({
method: "POST",
url: "/typo3/dahmsfunc/ajaxupload",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
The Ajax call is not working. The variable msg contains the whole HTML of the TYPO3 backend. If I try to call the path as it is defined the AjaxRouter I get a 404.
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 12
-
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T11:42:20+00:00" > 11/18/25, 11:42 </span>
-
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T11:56:10+00:00" > 11/18/25, 11:56 </span>
And this is my AjaxController:
<?php namespace Mikelmade\Dahmsfunc\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; use Psr\Log\LoggerInterface; use TYPO3\CMS\Backend\Clipboard\Clipboard; use TYPO3\CMS\Backend\Routing\UriBuilder; use TYPO3\CMS\Backend\Template\ModuleTemplate; use TYPO3\CMS\Backend\Template\ModuleTemplateFactory; use TYPO3\CMS\Backend\Tree\View\PageTreeView; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Imaging\IconFactory; use TYPO3\CMS\Core\Imaging\IconSize; use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Log\LogLevel; use TYPO3\CMS\Core\Resource\FileRepository; use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Type\Bitmask\Permission; use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity; use TYPO3\CMS\Core\Utility\DebugUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; use Mikelmade\Dahmsfunc\Domain\Repository\ArtworksRepository; /** * Controller for the backend module * */ class AjaxController extends ActionController { /** @var array<string, mixed> */ private array $dahmsfuncConfig; public function __construct( protected readonly ModuleTemplateFactory $moduleTemplateFactory, protected readonly IconFactory $iconFactory, protected readonly ExtensionConfiguration $extensionConfiguration, protected readonly PasswordHashFactory $passwordHashFactory, protected readonly ResourceFactory $resourceFactory, protected readonly FileRepository $fileRepository, protected readonly ConnectionPool $connectionPool, protected readonly LoggerInterface $logger, protected readonly UriBuilder $backendUriBuilder, protected readonly ArtworksRepository $artworksrepo, ) {} public function uploadAction(ServerRequestInterface $request): ResponseInterface { $response = $this->responseFactory->createResponse() ->withHeader('Content-Type', 'application/json; charset=utf-8'); $response->getBody()->write( json_encode(['result' => 'test'], JSON_THROW_ON_ERROR), ); return $response; } } -
Andreas Kienast commented and changed status
NewNeeds Feedback <span data-processor="relativetime" data-value="2025-11-18T12:28:17+00:00" > 11/18/25, 12:28 </span>Hello Michael,
please note that the file must be called
Configuration/Backend/AjaxRoutes.php, notConfiguration/Backend/AjaxRouter.php. Also, please keep in mind to clear all caches afterwards.Kind regards
Andreas -
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T12:32:04+00:00" > 11/18/25, 12:32 </span>
Sorry,
Of course, I meant “Configuration/Backend/AjaxRoutes.php” and not “Configuration/Backend/AjaxRouter.php”.
I made a typo here. And I cannot tell how often I already cleared the cache. It does not have any effect.
-
Garvin Hicking changed description <span data-processor="relativetime" data-value="2025-11-18T12:33:37+00:00" > 11/18/25, 12:33 </span>
-
Andreas Kienast commented <span data-processor="relativetime" data-value="2025-11-18T12:45:40+00:00" > 11/18/25, 12:45 </span>
Hello Michael,
apparently the URL you’re calling is wrong, it has to be
typo3/ajax/dahmsfunc/ajaxupload(the/ajaxinfix is added by TYPO3). However, instead hard-coding the URL by yourself, consider using the JavaScript objectTYPO3.settings.ajaxUrlswhich contains all registered AJAX routes. In your case, the URL can be accessed viaTYPO3.settings.ajaxUrls.dahmsfunc_ajax_upload.Example:
$.ajax({ method: "POST", url: TYPO3.settings.ajaxUrls.dahmsfunc_ajax_upload, data: { name: "John", location: "Boston" } }) .done(function( msg ) { alert( "Data Saved: " + msg ); }); -
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T13:00:21+00:00" > 11/18/25, 13:00 </span>
Hi Andreas,
Thanks for your answer – I already tried this also.
And got the error that TYPO3.settings.ajaxUrls.dahmsfunc_ajax_upload is “undefined”. -
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T13:06:05+00:00" > 11/18/25, 13:06 </span>
I tried to call “/typo3/ajax/dahmsfunc/ajaxupload” – which still returns the TYPO3 backend.
-
Andreas Kienast commented <span data-processor="relativetime" data-value="2025-11-18T13:07:14+00:00" > 11/18/25, 13:07 </span>
Hi Michael,
in another attempt I’d try to remove the
_ajax_infix from your route name completely (so the identifier becomesdahmsfunc_upload).
I’ve found #107095 (which is fixed in TYPO3 v14) that caused issues if the route name containsajax_. -
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T13:18:43+00:00" > 11/18/25, 13:18 </span>
Hi Andreas,
found it out at the same time :-)
And now it works like charm.
-
Michael Perlbach commented <span data-processor="relativetime" data-value="2025-11-18T13:23:32+00:00" > 11/18/25, 13:23 </span>
issue can be closed.
-
Garvin Hicking changed status
Needs FeedbackClosed <span data-processor="relativetime" data-value="2025-11-18T21:30:05+00:00" > 11/18/25, 21:30 </span>
Also to note that using @typo3/core/ajax/ajax-request.js yields the same result: The HTML of the TYPO3 backend is returnd.