1

I have question about call to my module action via ajax.

I'd like call to class in my module via ajax. But best solution for me is call to clean class. Not extends Module.

I don't know hot can I make url without add article to database and add module to him.

I use JQuery instead mooTools but js framework is not important. Most important is call to php class by ajax.

I have ajax module. But if I call to ajax.php required is module id from tl_module table. I don't want use this table. (Ajax will be very often calling, I prefer to don't load all contao mechanism. It should be very fast).

Thanks in advance for answers.

110precent
  • 322
  • 4
  • 19

2 Answers2

2

I found the answer for Contao >3.x in a GitHub issuse(german)

At first do in your Front-end Template:

<script type="text/javascript">
var data = {};
data["REQUEST_TOKEN"] = "<?php echo REQUEST_TOKEN ?>";

$(document).ready(function(){

    $("#trigger").click(function(event){

      $.post(
            '<?php echo \Contao\Environment::get('requestUri')?>',
            data,
            function(responseText) {
                alert(responseText);

            }
        ).fail(function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown )});
        event.preventDefault();
    });
});</script>

Important is the - data["REQUEST_TOKEN"] -> if you do not add it, the POST-request will not reach your module:

public function generate()
{   
    if ($_SERVER['REQUEST_METHOD']=="POST" && \Environment::get('isAjaxRequest')) {
       $this->myGenerateAjax();
       exit;
    }
   return parent::generate();
}

//do in frontend
protected function compile()
{
 ...
}
public function myGenerateAjax()
{      
    // Ajax Requests verarbeiten
    if(\Environment::get('isAjaxRequest')) {
        header('Content-Type: application/json; charset=UTF-8');
        echo json_encode(array(1, 2, 3));
        exit;
    }
}

If you want to do the ajax via GET you do not need the reqest token but the jquery funktion $get();

Flummiboy
  • 591
  • 1
  • 7
  • 20
  • Nice one.. This surely deserves an upvote .. This seems to work on contao 3.5.17 (latest version stable till date ).. Thanx mate .. – DpEN Oct 04 '16 at 08:35
  • In Response sometime we get 204 status because of cache problem can give have a solution for that ? – Vishal Tanna May 11 '17 at 13:31
  • cut the cache?! what cache are you talking about? – Flummiboy May 11 '17 at 14:29
  • Sometime i got 204 status code after clear cache From ( Systemwartung >> Daten bereinigen >> Internen Cache leeren) it gives us 200 status with data. – Vishal Tanna May 12 '17 at 05:02
0

I would suggest you to use Simple_Ajax extension. In this case you dont need to use Database and you can do pretty much anything you can do normally with Jquery ajax calls. It works with Contao 2.11 and you can call your php class with it. I find it much easier to use than ajax.php .

You can get it from : https://contao.org/de/extension-list/view/simple_ajax.de.html

  1. Copy SimpleAjax.php to Contao's root folder.
  2. Go to [CONTAO ROOT FOLDER]/system/modules and create a php file like following :

    class AjaxRequestClass extends System
    {
    
       public function AjaxRequestMethod()
       {
    
          if ($this->Input->post('type') == 'ajaxsimple' )
          {
             // DO YOUR STUFF HERE 
             exit; // YOU SHOULD exit; OTHERWISE YOU GET ERRORS
    
          }
       }
    }
    
  3. Create a folder called config with a php file like following ( You can hook you class to TL_HOOKS with class name - class method, simple_ajax will execute you method whenever a ajax call is made ):

     $GLOBALS['TL_HOOKS']['simpleAjax'][] = array('AjaxRequestClass','AjaxRequestMethod'); // Klassenname - Methodenname
    
  4. Now you can easily make ajax calls with simply posting data to SimpleAjax.php:

    $.ajax({
    type: "POST",
    url:  "SimpleAjax.php",
    data: { type: "ajaxsimple" },
    success: function(result)
    {
     //DO YOUR STUFF HERE
    }