No Code Attached Yet bug
avatar arenamax
arenamax
12 Feb 2022

Ajax is not available by namespace!

Steps to reproduce the issue

Namespace from install xml
   <namespace path="src">Am\Module\Amap</namespace>
   ...
js
Joomla.request({
	url: "index.php?option=com_ajax&module=amap&method=getReloadLayoutParams&format=json",
	method: "POST",
	headers: {
		"Cache-Control" : "no-cache",
		"Content-Type" : "application/json"
	},
	onBefore: function (xhr){
		console.log("onBefore");
	},

	onSuccess: function (response, xhr){
		if (response !== ""){
			let res = JSON.parse(response);
			console.log(res.message);
		}
		console.log("onSuccess");
	},
					
        onError: function(xhr){
		console.log("onError");
	},

	onComplete: function (xhr){
		console.log("onComplete");
	}
});
AmapHelper.php

path 'modules/mod_amap/src/Helper/AmapHelper.php'

<?php
namespace Am\Module\AMAP\Site\Helper;

\defined( '_JEXEC' ) or die;

class AmapHelper
{
        public static function getReloadLayoutParamsAjax()
	{
		$html = array();
		$html['success'] = true;
		$html['message'] = 'Ok';
		$html['messages'] = '';
		$html['data'] = array();
		
		echo json_encode($html);
                exit;
	}
}

Expected result

JSON data

Actual result

Console log
onBefore
File mod_amap/helper.php does not exist. // ??? It doesn't exist anyway
onSuccess
onComplete

System information (as much as possible)

Joomla v. 4.1.0-rc4
PHP 7.4.27

Votes

# of Users Experiencing Issue
1/1
Average Importance Score
5.00

avatar arenamax arenamax - open - 12 Feb 2022
avatar arenamax arenamax - change - 12 Feb 2022
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 12 Feb 2022
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 12 Feb 2022
avatar arenamax arenamax - change - 12 Feb 2022
Title
[com_ajax] Ajax call error from the module
[4.x][com_ajax] Ajax call error from the module
avatar arenamax arenamax - edited - 12 Feb 2022
avatar arenamax arenamax - edited - 12 Feb 2022
avatar arenamax arenamax - change - 12 Feb 2022
The description was changed
avatar wedal
wedal - comment - 12 Feb 2022

I confirm the problem.

In /components/com_ajax/ajax.php

$moduleInstance = $app->bootModule('mod_' . $module, $app->getName());

no helper class returned for a helper file in a subdirectory /src/Helper.

As a solution can use:
https://github.com/dgrammatiko/invalidate-cache/blob/d19f7f1b6b375dfcc4417d84c8ac3697097a84fd/src/invalidatecache/services/provider.php

But that's not good, I think.

avatar dgrammatiko
dgrammatiko - comment - 12 Feb 2022

But that's not good, I think.

This is the Joomla 4 way which is not 100% B/C because of namespacing...

avatar wedal
wedal - comment - 18 Feb 2022

Creating a separate file in a module just to be able to use com_ajax is definitely a problem.

I don't understand why when com_ajax knows the exact location and file name of a module class, we have to load that class manually in a separate file.

Also, there is no documentation about this in the com_ajax usage description.

avatar Ruud68
Ruud68 - comment - 28 Mar 2022

Running into the same issue.

In J4 Module Helper are namespaced and in src directory. com_ajax is apparently not able to load this helper file and then falls back to J3 file locations > 'mod_module'/helper.php which is not correct.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37012.
avatar Ruud68
Ruud68 - comment - 28 Mar 2022

You can try this yourself on a J4 site: [yourdomain/index.php?option=com_ajax&module=articles_news&method=test&format=raw
(note: a Joomla core module!)

which results in:
RuntimeException: The file at mod_articles_news/helper.php does not exist.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37012.

avatar richard67 richard67 - change - 29 Mar 2022
Status New Closed
Closed_Date 0000-00-00 00:00:00 2022-03-29 10:18:52
Closed_By richard67
avatar richard67 richard67 - close - 29 Mar 2022
avatar richard67
richard67 - comment - 29 Mar 2022

Closing as having a pull request. Please test #37398 . Thanks in advance.

avatar rdeutz rdeutz - change - 31 Mar 2022
Status Closed New
Closed_Date 2022-03-29 10:18:52
Closed_By richard67
avatar rdeutz rdeutz - reopen - 31 Mar 2022
avatar Hackwar Hackwar - change - 17 Feb 2023
Labels Added: bug
avatar Hackwar Hackwar - labeled - 17 Feb 2023

Add a Comment

Login with GitHub to post a comment