Advertisement
AJAX in J4.x MVC
Moderators: ooffick, General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
-
- Joomla! Apprentice
- Posts: 8
- Joined: Fri Oct 15, 2021 12:31 pm
AJAX in J4.x MVC
Hi, I developed an admin component based on J4.x MVC docs (https://docs.joomla.org/J4.x:Developing ... ponent/de).
Now I try to use AJAX to prevent reloading the whole site. Unfortunately I can't find useful 'how to' informations. The J3.x MVC docs are not useful to me to translate that into a J4 MVC.
Thanks for support!
Now I try to use AJAX to prevent reloading the whole site. Unfortunately I can't find useful 'how to' informations. The J3.x MVC docs are not useful to me to translate that into a J4 MVC.
Thanks for support!
Advertisement
- pe7er
- Joomla! Master
- Posts: 25484
- Joined: Thu Aug 18, 2005 8:55 pm
- Location: Nijmegen, Netherlands
- Contact:
Re: AJAX in J4.x MVC
Some other Joomla 4 developer resources:
That second resource has a small page about AJAX: https://www.dionysopoulos.me/book/plg-com-ajax.html
Maybe it will help you a little further...
Maybe it will help you a little further...
Kind Regards,
Peter Martin, Global Moderator + Joomla 5.2 Release Manager
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com
Peter Martin, Global Moderator + Joomla 5.2 Release Manager
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com
-
- Joomla! Apprentice
- Posts: 8
- Joined: Fri Oct 15, 2021 12:31 pm
Re: AJAX in J4.x MVC
Thanks dude. But...
Astrids blog is a well known ressource to me, but there's no information about AJAX calls.
The component com_ajax won't help too. First, this is only for modules and plugins - and I try to realize it in the MVC itself. Second, there's no hint how to supress all the header stuff generated by Joomla component to return just a json string.
I don't get why the Joomla developers shut down J3 while not having a complete documentation for J4.
Astrids blog is a well known ressource to me, but there's no information about AJAX calls.
The component com_ajax won't help too. First, this is only for modules and plugins - and I try to realize it in the MVC itself. Second, there's no hint how to supress all the header stuff generated by Joomla component to return just a json string.
I don't get why the Joomla developers shut down J3 while not having a complete documentation for J4.
-
- Joomla! Virtuoso
- Posts: 3253
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: AJAX in J4.x MVC
Are you asking about HTTP headers or about HTML content such as the template header being rendered in the response?
-
- Joomla! Apprentice
- Posts: 8
- Joined: Fri Oct 15, 2021 12:31 pm
Re: AJAX in J4.x MVC
Just want to make an AJAX call inside of my admin component to refresh some data. So my component should'nt output any rendered HTML, just make a DB request, prepare the content and return it as an JSON string.
I thought about adding a parameter to the call like:
?option=com_mycomponent&view=panel&format=json
or sth like that. Maybe there's a better way by creating a separate ajax.php file for handling just such requests. But where and what should be inside? Maybe its both - a file and a special URI call (by passing the correct parameter) and J4 is doing the rest for me (thats what I hope
)
I thought about adding a parameter to the call like:
?option=com_mycomponent&view=panel&format=json
or sth like that. Maybe there's a better way by creating a separate ajax.php file for handling just such requests. But where and what should be inside? Maybe its both - a file and a special URI call (by passing the correct parameter) and J4 is doing the rest for me (thats what I hope

-
- Joomla! Virtuoso
- Posts: 3253
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: AJAX in J4.x MVC
Adding format=json parameter is correct if you want to use JSON output.
No, creating custom entry points is generally a bad practice.Maybe there's a better way by creating a separate ajax.php file for handling just such requests
-
- Joomla! Apprentice
- Posts: 8
- Joined: Fri Oct 15, 2021 12:31 pm
Re: AJAX in J4.x MVC
After a couple of tries, I think I got it!
1. add &format=json to the URI
2. create a file JsonView.php in com_mycomponent/src/View/Viewname and insert following code
3. pass everything you want by using POST or GET and use jQuery.ajax
Now its time to insert tokencheck and content
1. add &format=json to the URI
2. create a file JsonView.php in com_mycomponent/src/View/Viewname and insert following code
Code: Select all
<?php
namespace [VENDOR]\Component\[ComponentName]\Administrator\View\[ViewName] ;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\View\JsonView as BaseJsonView;
class JsonView extends BaseJsonView {
function display($tpl = null) {
echo 'whatever you want';
}
}
Now its time to insert tokencheck and content
- Per Yngve Berg
- Joomla! Master
- Posts: 31843
- Joined: Mon Oct 27, 2008 9:27 pm
- Location: Romerike, Norway
Re: AJAX in J4.x MVC
Mod. Note: Not related to Extensions. Relocated the topic to the coding forum.
- ceford
- Joomla! Virtuoso
- Posts: 3281
- Joined: Mon Feb 24, 2014 10:38 pm
- Location: Edinburgh, Scotland
- Contact:
Re: AJAX in J4.x MVC
Have a look at this example: https://docs.joomla.org/J4.x:Joomla_4_T ... l_Box_Data
In essence you use JavaScript to fetch data from the server and place it wherever you want in the page. I use the async method a lot to fetch individual data items to update information on request.
In essence you use JavaScript to fetch data from the server and place it wherever you want in the page. I use the async method a lot to fetch individual data items to update information on request.
Advertisement