Advertisement

AJAX in J4.x MVC

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

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.
Locked
KriZan
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Oct 15, 2021 12:31 pm

AJAX in J4.x MVC

Post by KriZan » Wed Oct 11, 2023 7:32 am

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!

Advertisement
User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 25484
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: AJAX in J4.x MVC

Post by pe7er » Wed Oct 11, 2023 7:40 am

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...
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

KriZan
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Oct 15, 2021 12:31 pm

Re: AJAX in J4.x MVC

Post by KriZan » Wed Oct 11, 2023 8:08 am

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.

SharkyKZ
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3253
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: AJAX in J4.x MVC

Post by SharkyKZ » Wed Oct 11, 2023 8:46 am

Are you asking about HTTP headers or about HTML content such as the template header being rendered in the response?

KriZan
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Oct 15, 2021 12:31 pm

Re: AJAX in J4.x MVC

Post by KriZan » Wed Oct 11, 2023 9:35 am

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 :D)

SharkyKZ
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3253
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: AJAX in J4.x MVC

Post by SharkyKZ » Wed Oct 11, 2023 11:06 am

Adding format=json parameter is correct if you want to use JSON output.
Maybe there's a better way by creating a separate ajax.php file for handling just such requests
No, creating custom entry points is generally a bad practice.

KriZan
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Oct 15, 2021 12:31 pm

Re: AJAX in J4.x MVC

Post by KriZan » Wed Oct 11, 2023 11:28 am

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

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';
	}
}
3. pass everything you want by using POST or GET and use jQuery.ajax

Now its time to insert tokencheck and content

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 31843
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: AJAX in J4.x MVC

Post by Per Yngve Berg » Wed Oct 11, 2023 11:58 am

Mod. Note: Not related to Extensions. Relocated the topic to the coding forum.

User avatar
ceford
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3281
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: AJAX in J4.x MVC

Post by ceford » Wed Oct 11, 2023 8:23 pm

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.

Advertisement

Locked

Return to “Joomla! 4.x Coding”