【问题标题】:Ajax Request in Typo3 v10: Calling controller action via typeNumTypo3 v10 中的 Ajax 请求:通过 typeNum 调用控制器操作
【发布时间】:2021-06-20 19:53:07
【问题描述】:

我正在尝试通过 Javascript/Ajax 调用 Extbase 控制器操作。 版本是 Typo3 v10。控制器操作应该返回 JSON。

我已经创建了控制器和动作如下:

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;    
    }
}

接下来,我在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',
            ]
        );

接下来,我创建了一个文件 /theextension/Configuration/Typoscript/setup.typoscript,如下所示:

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        
}

我正在通过在浏览器和 Postman 中访问以下 URL 进行测试:

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

响应为 200 OK 且为空。我的控制器操作中的“error_log”行没有被调用。 如果我将 TypoScript 的最后一行更改为:

10 = TEXT
10.value = ExampleText

我确实返回了“ExampleText”。所以 TypoScript 定义、typeNUM 和上面的 URL 是有效的,但我不能让它调用控制器动作。我也试过: 10

谁能帮我指出正确的方向? 非常感谢您的帮助。

【问题讨论】:

    标签: ajax typoscript extbase typo3-10.x


    【解决方案1】:

    尝试在您的 TypoScript 代码中添加 pluginName、extensionName 和 vendorName。它应该是这样的:

    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
       }
    }
    

    也许这个答案会让你得到想要的结果

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

    【讨论】:

    • 非常感谢,成功了!小警告:我无法通过 TypoScript 控制控制器的操作。不过,对于两个不同的操作,我创建了两个不同的插件并定义了两个单独的 typeNum 页面定义。
    猜你喜欢
    • 2014-08-03
    • 2021-06-16
    • 1970-01-01
    • 2020-10-16
    • 2013-09-16
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多