Ajax callback from a module
-
- Joomla! Intern
- Posts: 55
- Joined: Mon Feb 11, 2008 3:49 pm
- Contact:
Ajax callback from a module
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?
/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?
-
- Joomla! Guru
- Posts: 907
- Joined: Sun Dec 21, 2008 6:35 pm
- Location: USA
Re: Ajax callback from a module
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
dpminusa
-
- Joomla! Intern
- Posts: 55
- Joined: Mon Feb 11, 2008 3:49 pm
- Contact:
Re: Ajax callback from a module
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?
<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?
-
- Joomla! Guru
- Posts: 907
- Joined: Sun Dec 21, 2008 6:35 pm
- Location: USA
Re: Ajax callback from a module
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
If you get "restricted" then the framework is blocking you. They set a flag in the root index.php to control that.
dpminusa
-
- Joomla! Intern
- Posts: 55
- Joined: Mon Feb 11, 2008 3:49 pm
- Contact:
Re: Ajax callback from a module
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.dpminusa wrote:If you get "restricted" then the framework is blocking you. They set a flag in the root index.php to control 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
-
- Joomla! Guru
- Posts: 907
- Joined: Sun Dec 21, 2008 6:35 pm
- Location: USA
Re: Ajax callback from a module
// 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
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
-
- Joomla! Intern
- Posts: 55
- Joined: Mon Feb 11, 2008 3:49 pm
- Contact:
Re: Ajax callback from a module
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!
I'll do that then! Just wanted to make sure there's no better/formal way to do something like this.
Thanks!
-
- Joomla! Guru
- Posts: 907
- Joined: Sun Dec 21, 2008 6:35 pm
- Location: USA
Re: Ajax callback from a module
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
I have written my AJAX along the same lines as you.
Good luck. You sound like a very capable guy.
dpminusa
-
- Joomla! Enthusiast
- Posts: 146
- Joined: Tue Nov 08, 2005 9:32 am
- Location: Malaysia
Re: Ajax callback from a module
Hi,
regarding your second option:
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
regarding your second option:
, maybe you can refer to the post: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...
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
-
- Joomla! Intern
- Posts: 55
- Joined: Mon Feb 11, 2008 3:49 pm
- Contact:
Re: Ajax callback from a module
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
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