简体   繁体   中英

Typo3 6.2 eid AjaxDispatcher ignores Controll and action

I'm trying to get my Ajax-Function to call a certain controller inside my Typo3 extension. After several attempts, I decided to go with the same Ajax-Dispatcher that powermail uses. It does show an output, but it completely ignores the supposed controller and action. There are two Controllers in my Extension and so far three actions: 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();

Ajax:

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

Instead of showing family->compare the Ajax puts out category->compare. I don't understand why.

Can someone please help? I'm working on this problem for over 2 days now...

I don't know exactly what you mean. If you want to trigger an AJAX request while clicking on a link you can do the following in the belonging view of your action. In this example the script will output the AJAX response.

1) Create a ViewHelper containing something like that:

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

2) Call the ViewHelper in your view template and add a output container div with an id.

3) Implement the JavaScript for AJAX call

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM