Hi i am having problem to call another file by ajax from module.As i don't want to make a component to call it for ajax from module, so what i am thinking is calling index.php file from ajax passing a task variable and catching that variable in module main php file and returning data back to same module js ajax code. How much fruitful is this way and how do i return json data in best way? And any security risks?

Edit: this way i am calling ajax

        url:"<?php echo JURI::current(); ?>",
        type: 'GET',
           ajax: "true",                    
           sid: (Math.floor(Math.random() * 900000) + 100000)            
        }, ....

And in module's main php file -

$task = JRequest::getVar('ajax');
  //for test 
  echo "hurray"; exit; 

If there are best error free ways then let me know.

  • If AJAX doesn't return anything sensitive data you musn't be worry
    – turson
    Jul 1 '14 at 7:22
  • You say your module is going to be commercial and therefore available for people to use on their own sites. Is this module going to be for Joomla 2.5 only? Cause I would strongly suggest you do not use JRequest as it's deprecated
    – Lodder
    Jul 1 '14 at 14:19
  • yes say it's only for joomla 2.5 right now. But how fruitful is the url setting url:"<?php echo JURI::current(); ?>", in ajax call. Are there any drawbacks?
    – dev-m
    Jul 1 '14 at 14:24

Nowadays, the simplest way to work with Ajax from modules is implementing com_ajax. You can just install it and talk with com_ajax from any other extension.

Furthermore, you are working fully integrated with Joomla API, and you are in a far better position than installing a raw index.php (besides a security risk, there are measures that restrict this raw access e.g. .htaccess, therefore you would have to deactivate them. In conclusion, not recommended)

Joomla! Ajax Interface https://github.com/Joomla-Ajax-Interface/component

It's included in Joomla 3.2 and superior, and it's installable on Joomla 2.5.

Hello Ajax World https://www.youtube.com/watch?v=Gyc1qwaUQoY&list=PLSwFVRVZ3joTo3InkU_FT65_AK8dQiyZ4&index=40

  • I do know joomla core classes and use and i can implement com_ajax but my that module is a commercial module which many users will install, so it not looks good to tell them install com_ajax too.
    – dev-m
    Jul 1 '14 at 13:10
  • You can include in your module installer a script to install com_ajax. It's the best option to avoid a support nightmare caused by distributing a raw index.php.
    – Anibal
    Jul 2 '14 at 14:03
  • Can you please elaborate bit more about how and why a raw index.php affects module's json datatype ajax call.my simple ajax call i mentioned works well but not tested all the way.
    – dev-m
    Jul 2 '14 at 16:40
  • A raw index.php can work perfectly well in your site. But it's not a standard practice. Thus, if you copy the file in some directory, a hardened .htaccess most probably block the access from a call.
    – Anibal
    Jul 2 '14 at 16:47
  • Does com_ajax works in 2.5 now github.com/Joomla-Ajax-Interface/component?
    – dev-m
    Jul 2 '14 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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