[TYPO3-german] Re: AJAX im Plugin mit eID - Zugriff auf Session

David Brünner david.bruenner at t-online.de
Sun Jun 18 19:52:23 CEST 2017


:-D JIPIII :-D

Ich habs!!!!

Der einzige Fehler war die Codierung der URL.
So sieht meine Index.html aus:
<script>
var ajaxUrl = <f:uri.action action="session" pageType="99"/>
</script>

An folgenden URL geht dann der Ajax-Request: xxx?id=1&type=99&...
Der muss aber heißen: xxx?id=1&type=99&
==> & NICHT &

Komisch ist nur das mir dann TYPO3 garnicht mekert.
Auch nix von wegen: Session nicht definiert (wenn der Eintrag für sessionAction in der ext_localconf.php fehlt).

Hier mein (fast) vollständiger Code:
IndexController:
<?php
namespace KKSoftware\Kksession\Controller;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class IndexController extends ActionController {
	public function indexAction() {

	}

	public function sessionAction() {
		/** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $TSFE */
		$TSFE = $GLOBALS['TSFE'];
		$mode = GeneralUtility::_POST('mode');
		$data = null;
		switch ($mode) {
			case 'schreibe':
				$sesVal = GeneralUtility::_POST('sesVal');
				$TSFE->fe_user->setKey('ses', 'sesVal', $sesVal);
				break;
			case 'lese':
				$data = $TSFE->fe_user->getKey('ses', 'sesVal');
				break;
		}
		return json_encode(['sesVal' => $data]);
	}
}

Index.html
<script>
	var ajaxUrl = '<f:uri.action action="session" pageType="99"/>';
</script>

session.js
$().ready(function () {
	ajaxUrl = ajaxUrl.replace(/&/g, '&');

	$('button').on('click', function() {
		var mode = $(this).attr('data-mode');
		$.ajax({
			url: ajaxUrl,
			method: 'POST',
			dataType: 'json',
			data: {
				mode: mode,
				sesVal: $('#test').val()
			},
			success: function (result) {
				console.log(result);
			},
			error: function () {
				console.log('error');
			}
		});
	});
});


More information about the TYPO3-german mailing list