Search code examples
phpajaxjoomlajoomla4

Using Ajax to Load Data from Joomla Component


I have been using Joomla 1.5, using the below code to get data from the controller file and it works well.

Index.html :

$.ajax({
    url         : 'index.php?option=com_users&task=getData',
    success     : function(data){
        console.log(data);
    }
});

I have a function like this in components/com_users/controller.php file (joomla 1.5) :

public static function getData(){
    echo 22;exit;
}

But with joomla 4.3.4, they changed the components' structure and were unsure how to get into work.

This is how the joomla 4.3.4 component looks and added same function in this file:
components/com_users/src/controller/UserController.php

I am getting this error enter image description here

Any help?


Solution

  • Modifiying core files is not recommended when building on the Joomla! framework, as the modifications may be overwritten when an update occurs.

    Having said that, in J4! you need to include the name of the controller in the task variable. The 404 error is being generated because Joomla! doesn't know which controller to send the request to and will default to using the DisplayController.

    Alter the url you are calling from:

    index.php?option=com_users&task=getData
    

    to:

    index.php?option=com_users&task=user.getdata
    

    and this should resolve the 404