-->

How to get an AJAX call in my TYPO3 Extbase Extens

2019-09-19 10:11发布

问题:

I have trouble doing this, I've been looking for some manuals, but they don't seems to work for me (they're either or detailed enough or I'm too stupid, probably something from both).

So I want to start an AJAX call from my Fluid View HTML file by some event, then it should go for the action in some controller and return some value (any value would be fine for the moment, I just can't seem to get anything to work here when it comes to AJAX in Extbase).

回答1:

With no details I can only guess what is your problem

First and most important thing is ... using FireBug to check what the problem with AJAX call is. It can be just wrong URL (action, controller etc...) or some problem in your action which doesn't return required output. FireBug will allow you to check where it stacks.

But most probably...

There is VERY important thing to remember while writing JS/CSS code directly into your Fluid view and unfortunately it's almost never mentioned:

Fluid uses braces to indicate that the string is a variable: {someVar}, therefore if you put AJAX or other JavaScript function into the Fluid template it will be most probably considered just as Fluid variable. This sample pasted to the template:

$.ajax({
  url: "index.php"
  data: "tx_myextension_plugin[controller]=MyController&tx_myextension_plugin[action]=getData&type=89657201"
  success: function(result) {
    alert(result);
  }
});       

Will be rendered just as:

$.ajax(Array);

Solution:

Escaping it directly in the template is hard, therefore it's definitely easier just to save all your JavaScript into separate file under Resources/Public directory of your extension, and include it with common HTML tag:

<script type="text/javascript" src="typo3conf/ext/yourext/Resources/Public/ajaxFunctions.js"></script>


回答2:

$("#searchFach").change(function(){
        $.ajax({
              type: 'POST',
              url:"fragen/controller/Question/searchQuestions",      
              data: {mainCategoryId:$("#searchFach").val()},
              success: function(response) { $("#searchCategory").html(response);                         
              },   
        });
    });`