0

我试图让我的 Ajax-Function 调用我的 Typo3 扩展中的某个控制器。经过几次尝试,我决定使用与 powermail 相同的 Ajax-Dispatcher。它确实显示了一个输出,但它完全忽略了假定的控制器和操作。我的扩展中有两个控制器,到目前为止三个动作:Category=>list Family=>list,compare

PHP:

<?php
namespace Vendor\Myextension\Utility;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
class AjaxDispatcher {

/**
 * configuration
 *
 * @var \array
 */
protected $configuration;

/**
 * bootstrap
 *
 * @var \array
 */
protected $bootstrap;

/**
 * Generates the output
 *
 * @return \string  rendered action
 */
public function run() {
    return $this->bootstrap->run('', $this->configuration);
}

/**
 * Initialize Extbase
 *
 * @param \array $TYPO3_CONF_VARS The global array. Will be set internally
 */
public function __construct($TYPO3_CONF_VARS) {
    $this->configuration = array(
        'pluginName' => 'my_plugin',
        'vendorName' => 'Vendor',
        'extensionName' => 'myextension',
        'controller' => 'Family',
        'action' => 'compare',
        'mvc' => array(
            'requestHandlers' => array(
                'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler' => 'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler'
            )
        ),
        'settings' => array()
    );
    $_POST['request']['action'] = 'compare';
    $_POST['request']['controller'] = 'Family';

    $this->bootstrap = new \TYPO3\CMS\Extbase\Core\Bootstrap();

    $userObj = \TYPO3\CMS\Frontend\Utility\EidUtility::initFeUser();
    $pid = (GeneralUtility::_GET('id') ? GeneralUtility::_GET('id') : 1);
    $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
        'TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController',
        $TYPO3_CONF_VARS,
        $pid,
        0,
        TRUE
    );
    $GLOBALS['TSFE']->connectToDB();
    $GLOBALS['TSFE']->fe_user = $userObj;
    $GLOBALS['TSFE']->id = $pid;
    $GLOBALS['TSFE']->determineId();
    $GLOBALS['TSFE']->getCompressedTCarray();
    $GLOBALS['TSFE']->initTemplate();
    $GLOBALS['TSFE']->getConfigArray();
    $GLOBALS['TSFE']->includeTCA();
}
}
$eid = GeneralUtility::makeInstance('Vendor\Myextension\Utility\AjaxDispatcher', $GLOBALS['TYPO3_CONF_VARS']);
echo $eid->run();

阿贾克斯:

var read = 'string';
var requestData = {'value': read};
var currentUrl = window.location;
$.ajax({
url: currentUrl,
type: 'POST',
data: {
    eID: "ajaxDispatcherMyextension",
    request: {
        controller: 'Family',
        action: 'compare',
        arguments: {
            'test': requestData
        }
    }
},
dataType: 'html',
success: function(success) {
    console.log('success ' + success);
    $('#test').html(success);
}
});

Ajax 没有显示family->compare,而是显示category->compare。我不明白为什么。

有人可以帮忙吗?我现在正在解决这个问题超过 2 天...

4

2 回答 2

0

我不知道你到底是什么意思。如果您想在单击链接时触发 AJAX 请求,您可以在操作的所属视图中执行以下操作。在此示例中,脚本将输出 AJAX 响应。

1)创建一个包含类似内容的 ViewHelper:

 $uriBuilder = $this->controllerContext->getUriBuilder();
 $uri = $uriBuilder->uriFor(
                'title', 
                array("param1" => $value1, "param2" => $value2), 
                '<controllerName>', 
                '<extKey>', 
                '<pluginName>');
 return '<a id="link" href="'.$uri.'">';

2) 在视图模板中调用 ViewHelper 并添加一个带有 id 的输出容器 div。

3) 为 AJAX 调用实现 JavaScript

function loadurl(dest, obj) {
    try {
        xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        console.log(e);
    }
    xmlhttp.onreadystatechange = function() {
        triggered(obj);
    };

    xmlhttp.open("GET", dest);
    xmlhttp.send(null);
}

function triggered(obj) {

    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
        document.getElementById(obj).innerHTML = xmlhttp.responseText;
    }
}

window.addEventListener("load", function() {
    var item = document.getElementsById('link');
    item.addEventListener('click', function(event) {
        event.preventDefault();
        var href = this.childNodes[1].getAttribute("href");
        loadurl(href, 'idOfOutputContainer');
    }
}
于 2015-02-19T09:37:13.543 回答
0

如我的错误报告中所述,它未在 6.2 中实现:RequestBuilder.php:loadDefaultValues 中未使用的操作和控制器

于 2017-03-18T21:51:11.397 回答