0

As joomla 3x has inbuilt ajax instead of calling an external script, can joomla inbuilt ajax system be used to fetch data from table without refreshing page

I mean if there is a table having company field like apple, samsung then on selecting apple can display in iphone11,iphone12,iphone13,iphone14. Whats the path of ajax script which is inbuilt in joomla

1

1 Answer 1

1

You don't have to use com_ajax, in fact I find it easier to do a Fetch directly to a View or Controller in my extensions and return the data to the calling java script. What follows is a sample of something I am working on at the moment to give you an idea what is involved, however it has been heavily edited to appear here so don't expect it to be working code you can just cut and paste but it would be close.

This JS uses the browser Fetch API rather than the older 'ajax' approach, https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API, which I find a lot easier to work with.

async function addToTrolley(fred) {
    let url = 'index.php?option=com_mycomponent&task=mycontroller.incoming&format=json';
    const token = Joomla.getOptions('csrf.token', '');
    let data = new URLSearchParams();
    data.append('payload', fred);
    data.append(token, 1);

    const options = {
        method: 'POST',
        body: data
    }

    //makes request to My Component to retrieve the item record.
    let response = await fetch(url, options);

    if (!response.ok) {
        throw new Error(Joomla.Text._('COM_MYCOMPONENT_JS_ERROR_STATUS') + `${response.status}`);
    } else {
        results = await response.json();
        console.log(results);
        return results;
    }

In mycontroller I receive the options from the Input, call up my model to get the data and then return it to the caller.

public function incoming()
{ 
    // Get the details of the required data
    $payload = $this->app->input->get('payload', '', 'string');
    $options = json_decode($payload, true);
    
    // create an instance of the Model
    $model   = parent::getModel('mymodel','Site', $options);
    
    // Get message from model to be passed back to the caller.
    $response = new JsonResponse($model->data);
    echo $response;
}

With Joomla 4 you also have the option of creating an API Application for your extension(s) that provides the similar functionality. However the API connection currently requires the Token of a Super User to provide authentication so I prefer not to use that approach on a public facing page where naughty people could access the Token stored in your JS and and only use it for internal processes where registered users already have a high level of access.

It is a use case type decision of which approach to use in each circumstance, but in Joomla 4 I don't see a need for com_ajax in the work I have done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.