具有以下用于ajax请求的TYPO3页面
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请求异步,则不会生成JSON数据。
这个案子出了什么问题?

发布于 2021-09-01 05:33:43
试试下面的代码,也许能帮到你。对我来说很管用。
<?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();
}希望这对你也有用!
https://stackoverflow.com/questions/69000570
复制相似问题