Ajax callback from a module

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
zak_the_man
Joomla! Intern
Joomla! Intern
Posts: 55
Joined: Mon Feb 11, 2008 3:49 pm
Contact:

Ajax callback from a module

Post by zak_the_man » Sun Feb 22, 2009 6:01 am

Working on a module which needs to update itself frequently. I know how to do a Ajax callback through Javascript and grab info from some other source and update a division's innerhtml. But what I need to do this time is to call the module's php in the Ajax callback. Is there a way to call a module through a URL, something like:

/index.php?option=mod_mymodule&task=query....

I know that works with components, but I can't get it to work with modules.

What is the proper way to do a Ajax callback to itself for a module?

dpminusa
Joomla! Guru
Joomla! Guru
Posts: 907
Joined: Sun Dec 21, 2008 6:35 pm
Location: USA

Re: Ajax callback from a module

Post by dpminusa » Sun Feb 22, 2009 6:21 am

What is the exact problem with the module code now. What message do you get? Is it an access denied or restricted or a PHP error code or ???

dpminusa

zak_the_man
Joomla! Intern
Joomla! Intern
Posts: 55
Joined: Mon Feb 11, 2008 3:49 pm
Contact:

Re: Ajax callback from a module

Post by zak_the_man » Sun Feb 22, 2009 2:53 pm

The template creates the simple layout with a division for the content, like this:

<div id="my_container">
Some initial content...
</div>

<script language="Javavascript">
// Ajax callback to my php file or some other php file in the module folder to get new data periodically here. Updates the innerHTML of my_container.
</script>


If I call the php file with the URL like this

/modules/mod_mymod/mod_mymod.php

then I do get the access denied error since I'm not going through the Joomla framework.

I can workaround it by not not having the access check in a php that receives the ajax callback, but I prefer not to if possible.

Is it possibe to call a php in the module with an Ajax callback like this though the main entry point of Joomla, like we can do with components, something like this?

/index.php?option=mod_mymod&view=callback&task=xyz


Or, is ther another proper way of calling it?

dpminusa
Joomla! Guru
Joomla! Guru
Posts: 907
Joined: Sun Dec 21, 2008 6:35 pm
Location: USA

Re: Ajax callback from a module

Post by dpminusa » Sun Feb 22, 2009 7:27 pm

In my JS scripts the Access denied is the browser security not the framework. I use a Perl proxy script to get around this. That way the domain name is the same as far as the browser sees. It is the cross-domain issue that causes the "access denied".

If you get "restricted" then the framework is blocking you. They set a flag in the root index.php to control that.

dpminusa

zak_the_man
Joomla! Intern
Joomla! Intern
Posts: 55
Joined: Mon Feb 11, 2008 3:49 pm
Contact:

Re: Ajax callback from a module

Post by zak_the_man » Sun Feb 22, 2009 9:08 pm

dpminusa wrote:If you get "restricted" then the framework is blocking you. They set a flag in the root index.php to control that.
Yes, I do get the "Restricted Access" error since I'm not going through the Joomla framework, I'm going directly to the module PHP file with the Ajax callback. Sorry for not beeing 100% clear on that.

What do you mean with ""set a flag in the root index.php to control that" ?

I need to be able to distribute this module to other users and I want the install to be automatic. Not so sure that adding something to the root index.php is a good idea.
  • Disable the security check in a PHP file that the Ajax callback is pointed to.
  • Create a companion component and have the users install both the module and the component. I can then point the Ajax callback to a URL like this:

    /index.php?option=com_mycomp&view=ajax
Don't really like either alternative, but if I have to I probably go with the second option - unless there is some better way of doing this.

dpminusa
Joomla! Guru
Joomla! Guru
Posts: 907
Joined: Sun Dec 21, 2008 6:35 pm
Location: USA

Re: Ajax callback from a module

Post by dpminusa » Sun Feb 22, 2009 9:45 pm

// Set flag that this is a parent file
define( '_JEXEC', 1 );

You need to acknowledge this in you code to be unrestricted.

// no direct access
defined( '_JEXEC' ) or die( 'Direct Access Prohibited' );

I am not saying add something I am saying the above code needs to be part of your module. All authors add it.

So the callback succeeds without "access denied" from the browser? Your explanation is not clear to me?

dpminusa

zak_the_man
Joomla! Intern
Joomla! Intern
Posts: 55
Joined: Mon Feb 11, 2008 3:49 pm
Contact:

Re: Ajax callback from a module

Post by zak_the_man » Sun Feb 22, 2009 10:02 pm

I do not have any issued with "Acecss Denied". I hit the PHP, but I get the "Restricted Access" error since I don't go through the Joomla Framework, e.g. '_JEXEC' is NOT set. I know I can define it in my module PHP and get passed the problem - that's what I meant with "Disable the security check in a PHP file that the Ajax callback is pointed to."

I'll do that then! Just wanted to make sure there's no better/formal way to do something like this.

Thanks!

dpminusa
Joomla! Guru
Joomla! Guru
Posts: 907
Joined: Sun Dec 21, 2008 6:35 pm
Location: USA

Re: Ajax callback from a module

Post by dpminusa » Sun Feb 22, 2009 11:03 pm

Not to my knowledge. Leave the thread open for a couple of days to encourage other ideas.

I have written my AJAX along the same lines as you.

Good luck. You sound like a very capable guy.

dpminusa

secteur
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 146
Joined: Tue Nov 08, 2005 9:32 am
Location: Malaysia

Re: Ajax callback from a module

Post by secteur » Tue Feb 24, 2009 3:57 am

Hi,

regarding your second option:
Create a companion component and have the users install both the module and the component. I can then point the Ajax callback to a URL...
, maybe you can refer to the post:
http://forum.joomla.org/viewtopic.php?f=231&t=312670
code is included too.
This component can be used to display any module, not just a particular one. Users would still need to install it, but it can then be used for displaying any number / type of module. I've been using it for some time because I have several modules that reload using Ajax. Dont want to create a helper (companion) component for each of them.

I was never sure whether this is the best / only way but it works and doesn't go out of the Joomla framework.

Hope this helps

Mat

zak_the_man
Joomla! Intern
Joomla! Intern
Posts: 55
Joined: Mon Feb 11, 2008 3:49 pm
Contact:

Re: Ajax callback from a module

Post by zak_the_man » Fri Mar 06, 2009 7:39 am

Thanks secteur!

I did decide to go down the component path anyway. Not just for the Ajax issue alone, but other things prompted me to do that so it took care of the problem. Keeps my application more secure and fully within the standard Joomla framework.

-Mats


Locked

Return to “Joomla! 1.5 Coding”