Typo3 v9 - Ajax 插件 JSON 响应为空

Typo3 v9 - Ajax Plugin JSON response is empty

有以下 TYPO3 页面 ajax 请求

ajaxAutocomplte_page = PAGE
ajaxAutocomplte_page {
    typeNum = 111871
    10 = COA_INT
    10 {
        userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
        extensionName= MyExt
        pluginName = AjaxAutocomplete
        vendorName = TYPO3
    }

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

并从 Controller

返回以下响应
 public function autocompleteJsonAction()
    {
        $query = $_GET['query'];

        $data = $this->templatesRepository->getAutocompleteData($query);

        $this->view->setVariablesToRender(['data']);
        $this->view->assign('data', $data);
    }

将在直接访问 URL 时生成 JSON 数据,但如果我通过 javascript

请求异步则不会

这种情况出了什么问题?

试试下面的代码也许能帮到你。它对我有用。

<?php

public function autocompleteJsonAction() {
    $query = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('query');
    $data = $this->templatesRepository->getAutocompleteData($query);

    $this->response->setHeader('Content-Type', 'application/json', true);
    $this->response->sendHeaders();

    $theView = $this->objectManager->get("TYPO3\CMS\Extbase\Mvc\View\JsonView");
    $theView->setControllerContext($this->controllerContext);
    $theView->assignMultiple([
        'items' => $data
    ]);
    $theView->setVariablesToRender(['items']);

    return $theView->render();
}

希望这对你也有用!