joomla component development with Ajax queries

前端 未结 3 2104
悲&欢浪女
悲&欢浪女 2021-01-15 15:26

I have a PHP script used for AJAX queries, but I want them to be able to operate under the umbrella of Joomla\'s (2.5) framework so I can have session id\'s, user id\'s etc

3条回答
  •  没有蜡笔的小新
    2021-01-15 16:02

    You don't need to create any custom files and add them into Joomla script. You just need a controller to serve ajax request. You don't even need a view (one way).

    Your ajax call should be like these:

    $(function () {
     $.ajax({                                     
       url: 'index.php?option=com_&no_html=1task=.',  //not_html = 1 is important since joomla always renders it's default layout with menus and everything else, but we want the raw json output            
       dataType: 'json' //data format     
       ...
     });
    }); 
    

    And your controller:

        /*
        * @file admin/controller/.php
        */
    
    class Controller extends JController
    {
    
            public function ()
            {
    
             //do something
             $respnse['message'] = 'Your message for the view';
             die(json_encode($reponse));
            }
        }
    
    ...
    

    This is just one of the examples of how it's could be done.

提交回复
热议问题