Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Hot answers tagged

47

PLEASE NOTE THAT THIS ANSWER is already a few years old and was not updated. Feel free to edit / comment if you think something is no longer exact. Abstract There is almost no really official way of dealing with this, it much depends on the complexity and how much you want to rely on the MVC pattern to do the work. Below are some possible solutions what ...


20

This is a late answer for this very well answered question, but I wanted to add this cut-to-the-chase solution for those that just need a simple way to get to their components's data with an AJAX call. With all the Joomla versions, 3rd party possibilities, and hacks that I found over several days of googling, this was the simplest approach I could come up ...


11

Valentin answer is good but is a bit overly complex if all you need to do is add 1 or 2 ajax calls to a component that is already built. It's perfectly possible to get away with not making separate controller.raw.php or view.raw.php files. To make this ajax call index.php?format=raw&option=com_example&controller=job&task=keep_alive&...


9

First of all you need to pass article ID with your request. Then you can use JTable class to update the state: public function onAjaxDeletearticle() { // Get id from the request $id = JFactory::getApplication()->input->getInt('data'); // Get the new instance of #__content table $table = JTable::getInstance('content'); // Load the ...


9

You should use Joomla's JInput class https://docs.joomla.org/Retrieving_request_data_using_JInput e.g. public function add() { // Get Joomla's input object: $input = JFactory::getApplication()->input; // Get the variable $value = $input->get('id'); $session = JFactory::getSession(); $cart = array(); $cart['item'] = array('id' => $...


7

Valentin's answer is good. I prefer a json controller that handles the encoding and error handling for this I created a json base class: class itrControllerJson extends JControllerLegacy { /** @var array the response to the client */ protected $response = array(); public function addResponse($type, $message, $status=200) { array_push($this-&...


7

According to my experience: JSON views: I have created components based FoF automatic JSON rendering https://github.com/akeeba/fof In this way, it is straightforward to load /get queries. In spite of this, certain tasks can simply be solved with a simple Controller function. With FoF, in a perfect case, you can call an Url (index.php?option=com_todo&...


6

I finally found what seems to be the "correct" way to do this. Even if I used sendHeaders, the additional Content-Type was being added and that got returned as it was added later. I followed through the code and AbstractWebApplication.php sets the headers with this line: // Send the content-type header. $this->setHeader('Content-Type', $this->...


6

My advice to you - avoid this. It's a recipe for many problems. A website owner who wanted this ended up paying us a lot of money just to fix the problems that arose from using this strategy (there were many, many issues). At the end, the website owner reverted back to a "non-ajax" template and never looked back again. Issues included: Many JavaScript ...


5

All the things mentioned by @Bakual are correct but I notice that you are just passing the result.message to alert and given that [alert(message)][1] expects message to be a string or an object it can convert to a string this could be the problem. If result.message is just a string, then you will need to wrap it in an appropriate container, something like ...


5

com_ajax is only really used if the data for the module is supported (JSON based), although it has other features. In some cased the easiest way to "just" load a module, is to create a new template file. This can be done by creating something like "module.php" in the template directory. Then using something like this inside. <?php $jinput = JFactory::...


5

I'm assuming your using JQuery. If your using Mootools let me know, and I'll update this. Your AJAX call can look something like this: $.ajax({ url : urlToSend, contentType : "application/json; charset=utf-8", type : 'get', cache : false, success : function(data) { successFunction(...


5

I managed to make it work. Sharing the same which can help others. No change on the form and controller. Slight changes on the model and ajax model public function uploadPicture() { $input = JFactory::getApplication ()->input; $formdata = $input->get ( 'jform', '', 'array' ); $filedata = $input->files->get ( 'jform' ); print_r($...


4

If you are 100% sure there's no thrid-party plugin adding any Javascript output, a pure json_encode works Ok. But ... for example JomSocial adds "" to the whole site. So... a handy trick, wrap json_encode with tags, and process it on Javascript side. echo '@START@' . json_encode(...) . '@END@';


4

Edit: It seams that the answer is not correct. The default Joomla Session Handler (Database) does not lock the session while processing. So you don't have to worry about locking. Anyway its possible that you override that session data of the first request with data of the second requests.


4

Initially I figured the best would be to use the controller method save(), which I finally got working like this: $path = JPATH_ADMINISTRATOR . '/components/com_anycomponent; JLoader::register('AnycomponentControllerAnyController', $path . '/controllers/anycontroller.php'); $controller = AnycomponentControllerAnyController::getInstance('Anycomponent'); $...


4

Well in case you don't want to use Joomla Ajax Interface, you could also do it like this: You have a module and you have your component. The script code goes into a javascript file or the view of the module (so that it will be invoked by the module). Then you build your Component. Your component only needs to be installable, it does not need any ...


4

I recently encountered this XMLHttpRequest error myself. The root of the problem caused 3 different issues. SOLUTION: In my case, the problem was caused by an incorrect setting for the public $live_site variable in the configuration.php file (which is located in the Joomla root folder). The website was https://website.com but I had set the public $...


4

For the ajax request to map to your module's helper the class must be named "modFilefinderHelper" From the documentation: Module requests must include the module variable in the URL, paired with the name of the module (i.e. module=session for mod_session). This value is also used for: The name of the directory to check for the helper file, e....


4

I wanted to answer your earlier question about this yesterday, but someone was already answered this partially for you. So according to me your original Login Form token field should be a bit changed like the following: <form method="post" action="#" id="userForm"> <label for="username">Brugernavn</label> <input type="text" ...


4

Playing with the sessions is usually not a very safe thing to do - you could create more problems by trying to find a solution. You can do one (or both) of the following: Increase the Session Lifetime under System, Global Configuration to a higher number, such as 1440 (1 day). This will minimize the problem, but will not eliminate it. Another problem with ...


4

You can use JSON format in the controller directly by adding .json suffix and changing format in your URL to json. In this case the controller would be placed in /administrator/components/com_example/controllers/ajax.json.php. It's contents: defined('_JEXEC') or die; use Joomla\CMS\Factory; use Joomla\CMS\Response\JsonResponse; class ExampleControllerAjax ...


3

To load the session you need to get it from JFactory. $session = JFactory::getSession(); Read more on How to access session variables set by an external script.


3

Took a little digging but I see the issue https://github.com/joomla-framework/application/blob/master/src/AbstractWebApplication.php If you look at the setHeader function it does not actually set the header yet, it only gets it "ready" So what you need to do is simple $this->app->setHeader('Content-Type', 'application/json', true)->sendHeaders();...


3

There is no easy solution, because bots have different behaviour. I would split them into 4 categories and solution for each: Legitimate crawlers (i.e. Google) - these usually respect robots.txt and as you said, you are not interested in that. Legitimate scrappers (i.e. Facebook) - these usually have the proper user agent, so you can block these based on ...


3

If you don't want content to be publicly accessible then use Joomla's ACL to restrict access, removing access for the default (public) usergroup. If you don't do that, or something similar, then your content is public, people can see it, bots can see it. If you don't want your content viewed by anyone, don't publish it to the world. If you do publish it to ...


3

One method would be to block accesses from crawlers, though this depends on being able to identify them. The following article has some good examples of how to enhance the Joomla security. http://docs.joomla.org/Htaccess_examples_%28security%29 One method is to use the HTTP_REFERER attribute to block access for example to images unless they are loaded ...


3

Welcome to the world of asynchronous calls. My advice is don't use sessions for the AJAX/RESTful calls at all. The most I would do is have some sort of authentication token so you know who is making the call.


3

You don't have to write your own JSession class, but you'll need to write a class extending JSessionStorage to integrate with Joomla's session handling. Most likely you'll run into trouble when trying to set the the session handler trough the Global Configuration. Either override JFormFieldSessionHandler with a version that includes your handler or just ...


3

You can access a controller directly by using the controller name in the task: index.php?option=com_similar&task=controller.abc&format=raw will call: controller.raw.php (return is raw) index.php?option=com_similar&task=controller.abc will call: controller.php ( return is html if you don't use die; )


Only top voted, non community-wiki answers of a minimum length are eligible