[Frage] Ajax: Error aber OK ??? TYPO3-Version: 8.7.1

  • getit getit
    R2-D2
    0 x
    112 Beiträge
    3 Hilfreiche Beiträge
    17. 06. 2017, 00:18

    Ich habe hier ein sehr merkwürdiges Verhalten.

    Warum funktioniert dieser Ajax-Request nicht:

    setup.ts

    1. ajax = PAGE
    2. ajax {
    3. typeNum = 99
    4. config {
    5. disableAllHeaderCode = 1
    6. additionalHeaders = Content-type:application/json
    7. admPanel = 0
    8. debug = 0
    9. }
    10. 10 < tt_content.list.20.kksession_kksession
    11. }

    IndexController:
    1. <?php
    2. namespace KKSoftware\Kksession\Controller;
    3.  
    4. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
    5.  
    6. class IndexController extends ActionController {
    7.  
    8. public function indexAction() {
    9. }
    10.  
    11. public function sessionAction() {
    12. return json_encode(['test' => 'wert']);
    13. }
    14.  
    15. }

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

    session.js:

    1. $().ready(function () {
    2. $.ajax({
    3. url: ajaxUrl,
    4. type: 'post',
    5. dataType: 'json',
    6. success: function(result) {
    7. alert(result);
    8. },
    9. error: function(result) {
    10. console.log('ERROR');
    11. console.log(result);
    12. }
    13. });
    14. });

    Die Developer Konsole liefert mir im Netzwerktab:

    Request URL:http://localhost/typo8/index.php?id=1&amp;type=99&amp;tx_kksession_kksession%5Baction%5D=session&amp;tx_kksession_kksession%5Bcontroller%5D=Index&amp;cHash=110588b4f0d2913288e34c9d28da1e0b
    Request Method:POST
    Status Code:200 OK

    Allerdings ist der Response nicht das erwartete JSON {test:wert} sondern der Inhalt meiner Webseite

    1. <!DOCTYPE html>
    2. <html lang="en">
    3.  
    4. <meta charset="utf-8">
    5. <!--
    6. This website is powered by TYPO3 - inspiring people to share!
    7. TYPO3 is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.
    8. TYPO3 is copyright 1998-2017 of Kasper Skaarhoj. Extensions are copyright of their respective owners.
    9. Information and contribution at https://typo3.org/
    10. -->
    11.  
    12. <title>root</title>
    13. <meta name="generator" content="TYPO3 CMS">
    14.  
    15. <link rel="stylesheet" type="text/css" href="/typo8/typo3temp/assets/css/20658bc1f2.css?1497650740" media="all">
    16.  
    17. </head>
    18.  
    19. <div id="c21" class="frame frame-default frame-type-list frame-layout-0"><script>
    20. var ajaxUrl = 'index.php?id=1&amp;type=99&amp;tx_kksession_kksession%5Baction%5D=session&amp;tx_kksession_kksession%5Bcontroller%5D=Index&amp;cHash=110588b4f0d2913288e34c9d28da1e0b';
    21. </script></div>
    22.  
    23. <script src="/typo8/typo3conf/ext/kksession/Resources/Public/Js/jquery-3.2.1.min.js?1497200563" type="text/javascript"></script>
    24.  
    25. <script src="/typo8/typo3conf/ext/kksession/Resources/Public/Js/session.js?1497650554" type="text/javascript"></script>
    26.  
    27. </body>
    28. </html>
    29. <!-- Cached page generated 17-06-17 00:05. Expires 18-06-17 00:05 -->
    30. <!-- Parsetime: -75ms -->

    Desweiteren bekomme ich nach dem Ajax-Request (der laut der Konsole ja erfolgreich war) nicht das erwartete alert() sondern den Inhalt für den Fehlerfall in der Konsole angezeigt.

    Was läuft da falsch?:'(

    Tags: Ajax

  • 1
  • getit getit
    R2-D2
    0 x
    112 Beiträge
    3 Hilfreiche Beiträge
    18. 06. 2017, 20:00

    :D Ich habs :D

    1. <?php
    2. namespace KKSoftware\Kksession\Controller;
    3.  
    4. use TYPO3\CMS\Core\Utility\GeneralUtility;
    5. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
    6.  
    7. class IndexController extends ActionController {
    8. public function indexAction() {
    9.  
    10. }
    11.  
    12. public function sessionAction() {
    13. /** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $TSFE */
    14. $TSFE = $GLOBALS['TSFE'];
    15. $mode = GeneralUtility::_POST('mode');
    16. $data = null;
    17. switch ($mode) {
    18. case 'schreibe':
    19. $sesVal = GeneralUtility::_POST('sesVal');
    20. $TSFE->fe_user->setKey('ses', 'sesVal', $sesVal);
    21. break;
    22. case 'lese':
    23. $data = $TSFE->fe_user->getKey('ses', 'sesVal');
    24. break;
    25. }
    26. return json_encode(['sesVal' => $data]);
    27. }
    28. }
    29. }

    1. ajax = PAGE
    2. ajax {
    3. typeNum = 99
    4. config {
    5. disableAllHeaderCode = 1
    6. additionalHeaders = Content-type:application/json
    7. admPanel = 0
    8. debug = 0
    9. }
    10. 10 < tt_content.list.20.kksession_kksessionstorage
    11. }

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

    1. $().ready(function () {
    2. ajaxUrl = ajaxUrl.replace(/&amp;/g, '&');
    3.  
    4. $('button').on('click', function() {
    5. var mode = $(this).attr('data-mode');
    6. $.ajax({
    7. url: ajaxUrl,
    8. method: 'POST',
    9. dataType: 'json',
    10. data: {
    11. mode: mode,
    12. sesVal: $('#test').val()
    13. },
    14. success: function (result) {
    15. console.log(result);
    16. },
    17. error: function () {
    18. console.log('error');
    19. }
    20. });
    21. });
    22. });

  • 1