Search code examples
ajaxtyposcriptextbasetypo3-10.x

Ajax Request in Typo3 v10: Calling controller action via typeNum


I am trying to call an Extbase controller action via Javascript/Ajax. The version is Typo3 v10. The controller action is supposed to return JSON.

I have created the controller and the action as follows:

namespace Thevendor\Theextension\Controller;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;

class ExampleController
{
   /** @var ResponseFactoryInterface */
   private $responseFactory;

   public function __construct(ResponseFactoryInterface $responseFactory)
   {
      $this->responseFactory = $responseFactory;
   }
  
    public function doSomethingAction(ServerRequestInterface $request): Response
    {       
       error_log("controller action got called!");
       $data = ['result' => 42];
       $response = $this->responseFactory->createResponse()
          ->withHeader('Content-Type', 'application/json; charset=utf-8');
       $response->getBody()->write(json_encode($data));
       return $response;    
    }
}

Next, I have registered a plugin in ext_localconf.php:

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
            'Thevendor.Theextension',
            'Testing',
            [
                \Thevendor\Theextension\Controller\ExampleController::class => 'doSomething',               
            ],
            // non-cacheable actions
            [
                \Thevendor\Theextension\Controller\ExampleController::class => 'doSomething',
            ]
        );

Next, I have created a file /theextension/Configuration/Typoscript/setup.typoscript that looks like this:

example_page = PAGE
example_page {
    typeNum = 776776

    config {
        disableAllHeaderCode = 1
        additionalHeaders = Content-type:application/json
        xhtml_cleaning = 0
        debug = 0
        no_cache = 1
        admPanel = 0
    }
    10 < plugin.theextension_testing        
}

I am testing this by accessing the following URL in the browser and in Postman:

/home?no_cache=1&pagetype=776776&tx_theextension_testing%5Baction%5D=doSomething&tx_theextension_testing%5Bcontroller%5D=Example&type=776776&cHash=f63ebddb3625ec605e51b6ba07cf0731

The response is 200 OK and empty. The line "error_log" in my controller action is not called. If I change the last line of my TypoScript to:

10 = TEXT
10.value = ExampleText

I do get "ExampleText" returned. So the TypoScript definition, typeNUM and resulting above URL are working, but I cannot get it to call the controller action. I have also tried: 10 < tt_content.list.20.theextension_testing but the result was the same.

Can anybody help pointing me in the right direction? I really appreciate your help.


Solution

  • Try to add the pluginName, extensionName and vendorName as well in your TypoScript code. It should look like this:

    ajaxSearch_page = PAGE
    ajaxSearch_page {
        typeNum = 776776
        10 = USER
        10.userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
        10.extensionName= ExtensionName
        10.pluginName = PluginName
        10.vendorName = Vendor
    
       config {
          disableAllHeaderCode = 1
          additionalHeaders = Content-type:application/json
          xhtml_cleaning = 0
          debug = 0
          no_cache = 1
          admPanel = 0
       }
    }
    

    Maybe this answer will get you the wished results

    https://stackoverflow.com/a/61637195/7162477