我為零售商/商店數(shù)據(jù)庫構(gòu)建了一個Typo3插件。每個商店都應(yīng)該有經(jīng)緯度。為了不必手動輸入每個緯度和經(jīng)度,我添加了一個按鈕(在TCA中帶有fieldcontrol)來自動輸入數(shù)據(jù)。當(dāng)您首先保存存儲,然后檢索位置數(shù)據(jù)時,效果非常好。但在存錢之前這樣做會更方便。因此,我想通過ajax觸發(fā)數(shù)據(jù)導(dǎo)入。我在Typo3文檔中找到了這個例子:https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/FormEngine/Rendering/Index.html#add-fieldcontrol-example
現(xiàn)在我面臨一個顯然與ajax控制器有關(guān)的問題。我得到了以下錯誤:“函數(shù)Vendor\MyExt\Controller\Ajax\LocateAddressController::locateAddressAction()的參數(shù)太少,在第91行/.../typo3/sysext/backend/Classes/Http/RouteDispatcher.php中傳遞了1,正好需要2”
以下是我的代碼節(jié)選:LocateAddress.js:
/**
* @param {int} id
*/
LocateAddress.import = function (id) {
$.ajax({
type: 'POST',
url: TYPO3.settings.ajaxUrls['locate-address'],
data: {
'id': id
}
}).done(function (response) {
if (response.success) {
top.TYPO3.Notification.success('Import Done', response.output);
} else {
top.TYPO3.Notification.error('Import Error!');
}
});
};
AjaxRoutes.php:
return [
'locate-address' => [
'path' => '/locate-address',
'target' => \Vendor\MyExt\Controller\Ajax\LocateAddressController::class . '::locateAddressAction'
],
];
LocateAddressController.php:
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class LocateAddressController
{
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function locateAddressAction(ServerRequestInterface $request, ResponseInterface $response)
{
$queryParameters = $request->getParsedBody();
$response->getBody()->write(json_encode(['success' => true]));
return $response;
}
}
后端控制器(
ResponseInterface $response
)的第二個參數(shù)已被TYPO3 9棄用(請參閱文檔),并被TYPO3 v10刪除。extbase文檔中的示例似乎已經(jīng)過時。只需刪除第二個參數(shù),并在方法中創(chuàng)建響應(yīng)對象以返回它。