Reading list Switch to dark mode

    Ajax Call from Plugins and Modules In Joomla

    Updated 27 May 2020

    Introduction

    Ajax makes it possible to create websites that are dynamic and fast.
    We can use Ajax to refresh a specific section of a page without reloading all the content or to validate single element without submitting the whole form.

    While developing an extension for Joomla we may encounter a task which requires jQuery Ajax call it may be for the dynamically changing content of an element or to get data based on user input or to perform some operation in the background.

    If we are developing a component then we do not have any issue as we can make Ajax request to any controller of the component but what if we need to create only a plugin or module we might stuck here.

    As in earlier Joomla version, we need to create our own component if we need Ajax call but Joomla! 3.2 includes an AJAX Interface.

    Basically, com_ajax is a component which serves as an interface for Ajax calls for standalone plugins and modules.

    Start your headless eCommerce
    now.
    Find out More

    Some Use Cases

    • First, A module/plugin that retrieves data from an external API
    • Second, A module/plugin that interacts with a component that you did not develop
    • Third, A module/plugin that requires data using Ajax, we can write ajax method in same module/plugin

    How does Ajax Call work for modules and plugins?

    First of all, when making Ajax call from module/plugin URL must be, index.php?option=com_ajax.

    Other Parameters:

    Required –

    option=com_ajax

    [module|plugin]=name

    format=[json|debug|raw]

    Optional –

    method=[method name] defaults is get if not provided.

    A request to ?option=com_ajax&module=search would call mod_search with results returned in the default format. Similarly ?option=com_ajax&plugin=search&format=json would trigger the onAjaxSearch with results returned in JSON.

    com_ajax for Modules

    For using Ajax call in Modules some requirements are:

    1. A method must be defined in helper.php
    2. The request must include a module variable in the URL.Example:module=search for mod_search
    3. Optionally method variable can be used in the URL to replace default get method. Example: method=myFunction this will trigger myFunctionAjax

    Example Request

    jQuery.ajax({
        url: 'index.php?option=com_ajax&module=search&method=getData',
        type: "post",
        success :function(response){
            console.log(response);
        }
    });
    

    Above code will trigger method getDataAjax in helper.php of search module.

    public static getDataAjax(){
        //search records
    }
    Note: function must be static otherwise warnings will be thrown.
    

    com_ajax for Plugins

    For using Ajax call in plugins some requirements are:

    1. The method name must start with onAjax. Example: onAjax[Name]
    2. The request must include a plugin variable in the URL.Example:plugin=getData this will trigger onAjaxgetData
    3. Optionally group variable can be used in the URL to specify plugin group.

    Example Request

    /**
     * WKSOCIALMESSAGE -Joomla Social Messaging
     *
     *
     * @category   Plugin
     * @package    Joomla
     * @author     WebKul software private limited 
     * @copyright  2010 WebKul software private limited
     * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
     * @version    GIT:1.0
     * @filesource http://store.webkul.com
     * @link       Technical Support:  webkul.uvdesk.com
     */
    jQuery.ajax({
        url: 'index.php?option=com_ajax&group=system&plugin=connectWksocialTwitterProfile&format=json',
        data:{wksocial_action:"validate"},
        type: "post",
        success: function(data){
            //handle success
        },
        error:function(data){
            //handle error
        }
    )};
    
    

    Above code will trigger method onAjaxConnectWksocialTwitterProfile in the plugin file.

    /**
     * [onAjaxConnectTwitterProfile trigger onAjax for login user profile]
     *
     * @since  v1.0
     * @return [string] [content to render ]
     */
    public function onAjaxConnectWksocialTwitterProfile()
    {
       //login user via twitter
    }
    

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content