2

This is my ajax query for calling a task in the controller (it works for Joomla 3 but doesn't work for Joomla 4):

$.ajax({
    type: "POST",
    url: "index.php?option=com_scrib&task=test&format=raw",
    data: { 'forum_Id': forum_Id  },
    success:function(data)
    {
        var opts = $.parseJSON(data);
        $.each(opts, function(i, d) {
            $('#jform_category_id').append('<option value="' + d.id + '">' + d.name + '</option>');
        });
    }
});

The location of the controller file in Joomla 3: components/com_scrib/controller.php

Joomla 4 has a different file structure. I'm not sure if I'm doing it right.

In Joomla 4: components/com_scrib/src/Controller/DisplayController.php

What the correct url should be in Joomla 4?

1
  • Welcome to JSE, waxman. Please take our tour. Commented Mar 8, 2021 at 22:14

2 Answers 2

1

You need to use task=controller.action where controller is whichever controller you want to call less 'Controller' (so MythingController.php contains class MythingController and you use mything in the call) and action is the name of the function to call, so I guess that could be 'test' in your case. My calls don't include type: "POST", and I don't remember why.

The format=raw part looks worrying. You should normally include a form token which can be sent either as a get or post variable. And it is often simple to submit a form in which you first set the form task (hidden) field to the value you want.

0
0

Here is how I successfully implemented user3402785's advice in AjaxController.php:

<?php

namespace Mycomponent\Component\Mycomponent\Site\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;

class AjaxController extends BaseController
{
    public function getstatus()
    {
        header('HTTP/1.1 202 Accepted', true, 202);
        $data = $this->input->get('data');
        echo $data;
    }
}

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.