Difference between revisions of "Using Joomla Ajax Interface"

From Joomla! Documentation

(Several markup changes.)
 
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<noinclude>{{version/tutor|3.2}}</noinclude>
+
<noinclude><languages /></noinclude>
<h1>What is The Joomla Ajax Interface (com_ajax)</h1>
+
<noinclude>{{Joomla version|version=3.2|time=and after|comment=<translate><!--T:37-->
<p>A slim, extensible component to act as an entry point for HTTP requests for stand alone modules and plugins, thus allowing for the potential of Ajax functionality in them. Com_ajax is generally used when you are not the developer of the component that the module or plugin is interacting with.</p>
+
series</translate>}}</noinclude>
<p><strong>NOTE</strong>: If you are a component developer, you do not need to use com_ajax to implement Ajax functionality in it. You can do so directly in your component.</p>
+
{{-}}
<p>Examples of use cases include, but are not limited to:
+
<translate>==What is The Joomla Ajax Interface (''com_ajax'')== <!--T:1--></translate>
<ul>
+
<translate><!--T:2-->
<li>A module that retrieves data from an external API</li>
+
A slim, extensible component to act as an entry point for HTTP requests for stand alone modules and plugins, thus allowing for the potential of Ajax functionality in them. ''com_ajax ''is generally used when you are not the developer of the component that the module or plugin is interacting with.</translate>
<li>A module that interacts with a component that you did not develop</li>
 
<li>A plugin that implement API like functionality to allow consumption of data from your site</li>
 
</ul>
 
</p>
 
  
<h1>Anatomy of an Ajax Request</h1>
+
<translate><!--T:3-->
 +
'''NOTE''' If you are a component developer, you do not need to use ''com_ajax'' to implement Ajax functionality in it. You can do so directly in your component.</translate>
  
<p><strong>Required</strong></p>
+
<translate><!--T:4-->
 +
Examples of use cases include, but are not limited to:
 +
*A module that retrieves data from an external API
 +
*A module that interacts with a component that you did not develop
 +
*A plugin that implement API like functionality to allow consumption of data from your site</translate>
  
<ul>
+
<translate>==Anatomy of an Ajax Request== <!--T:5--></translate>
<li><code>option=com_ajax</code></li>
 
<li><code>[module|plugin]=name</code></li>
 
</ul><p><strong>Optional</strong></p>
 
  
<ul>
+
<translate>===Required=== <!--T:6--></translate>
<li>
 
<code>format=[json|debug]</code> defaults to raw if omitted.</li>
 
<li>
 
<code>method=[custom fragment]</code> defaults to <code>get</code> if omitted.</li>
 
</ul><p><strong>Overview</strong></p>
 
  
<p>All requests begin with <code>?option=com_ajax</code>, which calls this extension, and must indicate the type of extension to call, and the data format to be returned.</p>
+
*''option=com_ajax''
 +
*''[module|plugin]=name''
 +
*''format=[json|debug|raw]''
  
<p>Additional variables and values used by your extension may also be included in the URL.</p>
+
<translate>===Optional=== <!--T:7--></translate>
 +
*''method=[custom fragment]'' <translate><!--T:8-->
 +
defaults to ''get'' if omitted.</translate>
  
<p>For example, a request to <code>?option=com_ajax&amp;module=session</code> would call <code>mod_session</code> with results returned in the default format. In contrast,<code>?option=com_ajax&amp;plugin=session&amp;format=json</code> would trigger the <code>onAjaxSession</code> plugin group with results returned in JSON.</p>
+
<translate>===Overview=== <!--T:9--></translate>
 +
<translate><!--T:10-->
 +
All requests begin with ''?option=com_ajax'', which calls this extension, and must indicate the type of extension to call, and the data format to be returned.
  
<h2>
+
<!--T:11-->
Module Support</h2>
+
Additional variables and values used by your extension may also be included in the URL.</translate>
  
<p><strong>Summary</strong></p>
+
<translate><!--T:12-->
 +
For example, a request to ''?option=com_ajax&amp;module=session'' would call ''mod_session'' with results returned in the default format. In contrast,''?option=com_ajax&amp;plugin=session&amp;format=json'' would trigger the ''onAjaxSession'' plugin group with results returned in JSON.</translate>
  
<p>Module support is accomplished by calling a method in the module's <code>helper.php</code> file.</p>
+
<translate>==Module Support== <!--T:13--></translate>
  
<p><strong>Details</strong></p>
+
<translate>===Summary=== <!--T:14--></translate>
  
<p>Module requests must include the <code>module</code> variable in the URL, paired with the name of the module (i.e. <code>module=session</code> for <code>mod_session</code>).</p>
+
<translate><!--T:15-->
 +
Module support is accomplished by calling a method in the module's ''helper.php'' file.</translate>
  
<p>This value is also used for:</p>
+
<translate>===Details=== <!--T:16--></translate>
  
<ul>
+
<translate><!--T:17-->
<li>The name of the directory to check for the helper file, e.g. <code>/modules/mod_session/helper.php</code>
+
Module requests must include the ''module'' variable in the URL, paired with the name of the module (i.e. ''module=session'' for ''mod_session'').</translate>
</li>
 
<li>The class name to call, e.g. <code>modSessionHelper</code>
 
</li>
 
</ul><p>Optionally, the <code>method</code> variable may be included to override the default method prefix of <code>get</code>. </p>
 
  
<p>NOTE: All methods must end in <code>Ajax</code>. For example, <code>method=mySuperAwesomeMethodToTrigger</code> will call <code>mySuperAwesomeMethodToTriggerAjax</code></p>
+
<translate><!--T:18-->
 +
This value is also used for:
  
<p>The [https://github.com/Joomla-Ajax-Interface/Ajax-Session-Module Ajax Session Module] is an example module that demonstrates this functionality.</p>
+
<!--T:19-->
 +
*The name of the directory to check for the helper file, e.g. ''/modules/mod_session/helper.php''
  
<h2>
+
<!--T:20-->
Plugin Response</h2>
+
*The class name to call, e.g. ''modSessionHelper''</translate>
  
<p><strong>Summary</strong></p>
+
<translate><!--T:21-->
 +
Optionally, the ''method'' variable may be included to override the default method prefix of ''get''.</translate>
  
<p>Plugin support is accomplished by triggering the <code>onAjax[Name]</code> plugin event.</p>
+
:<translate><!--T:22-->
 +
'''NOTE''' All methods must end in ''Ajax''. For example:</translate>
 +
:''method=mySuperAwesomeMethodToTrigger'' <translate><!--T:23-->
 +
will call</translate> ''mySuperAwesomeMethodToTriggerAjax''
  
<p><strong>Details</strong></p>
+
<translate><!--T:24-->
 +
The [https://github.com/Joomla-Ajax-Interface/Ajax-Session-Module Ajax Session Module] is an example module that demonstrates this functionality.</translate>
  
<p>Plugin requests must include the <code>plugin</code> variable in the URL, paired with the name of the plugin event, e.g. <code>plugin=session</code> for <code>onAjaxSession</code>.</p>
+
<translate>==Plugin Response== <!--T:25--></translate>
  
<p>This value is also used for:</p>
+
<translate>===Summary=== <!--T:26-->
  
<ul>
+
<!--T:27-->
<li>The plugin class name following the <code>plgAjax[Name]</code> convention.</li>
+
Plugin support is accomplished by triggering the ''onAjax[Name]'' plugin event.</translate>
<li>The plugin function name following the <code>onAjax[Name]</code> convention.</li>
 
</ul><p>The [https://github.com/Joomla-Ajax-Interface/Ajax-Latest-Articles Ajax Latest Articles plugin] is an example plugin that demonstrates this functionality.</p>
 
  
<h2>
+
<translate>===Details=== <!--T:28-->
Response Format</h2>
 
  
<p><code>format=[json|debug]</code> is an optional argument for the results format:</p>
+
<!--T:29-->
 +
Plugin requests must include the ''plugin'' variable in the URL, paired with the name of the plugin event, e.g. ''plugin=session'' for ''onAjaxSession''.</translate>
  
<ul>
+
<translate><!--T:30-->
<li>
+
This value is also used for:
<code>json</code> for JSON format</li>
+
 
<li>
+
<!--T:31-->
<code>debug</code> for human-readable output of the results.</li>
+
*The plugin class name following the ''plgAjax[Name]'' convention.
</ul>
+
*The plugin function name following the ''onAjax[Name]'' convention.
 +
*The default plugin group is ''ajax''. You can change it specifying the ''group'' request parameter. (From Joomla! 3.4)
 +
 
 +
<!--T:32-->
 +
The [https://github.com/Joomla-Ajax-Interface/Ajax-Latest-Articles Ajax Latest Articles plugin] is an example plugin that demonstrates this functionality.</translate>
 +
 
 +
<translate>==Response Format== <!--T:33--></translate>
 +
 
 +
<translate><!--T:34-->
 +
''format=[json|debug]'' is an optional argument for the results format:
 +
 
 +
<!--T:35-->
 +
*''json'' for JSON format
 +
*''debug'' for human-readable output of the results.</translate>
 +
 
 +
<translate><!--T:36-->
 +
[[Category:AJAX]]
 +
[[Category:JavaScript]]
 +
[[Category:Development]]</translate>

Latest revision as of 13:06, 30 November 2022

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎русский • ‎فارسی • ‎中文(台灣)‎
Joomla! 
≥ 3.2
series

What is The Joomla Ajax Interface (com_ajax)[edit]

A slim, extensible component to act as an entry point for HTTP requests for stand alone modules and plugins, thus allowing for the potential of Ajax functionality in them. com_ajax is generally used when you are not the developer of the component that the module or plugin is interacting with.

NOTE If you are a component developer, you do not need to use com_ajax to implement Ajax functionality in it. You can do so directly in your component.

Examples of use cases include, but are not limited to:

  • A module that retrieves data from an external API
  • A module that interacts with a component that you did not develop
  • A plugin that implement API like functionality to allow consumption of data from your site

Anatomy of an Ajax Request[edit]

Required[edit]

  • option=com_ajax
  • [module|plugin]=name
  • format=[json|debug|raw]

Optional[edit]

  • method=[custom fragment] defaults to get if omitted.

Overview[edit]

All requests begin with ?option=com_ajax, which calls this extension, and must indicate the type of extension to call, and the data format to be returned.

Additional variables and values used by your extension may also be included in the URL.

For example, a request to ?option=com_ajax&module=session would call mod_session with results returned in the default format. In contrast,?option=com_ajax&plugin=session&format=json would trigger the onAjaxSession plugin group with results returned in JSON.

Module Support[edit]

Summary[edit]

Module support is accomplished by calling a method in the module's helper.php file.

Details[edit]

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.g. /modules/mod_session/helper.php
  • The class name to call, e.g. modSessionHelper

Optionally, the method variable may be included to override the default method prefix of get.

NOTE All methods must end in Ajax. For example:
method=mySuperAwesomeMethodToTrigger will call mySuperAwesomeMethodToTriggerAjax

The Ajax Session Module is an example module that demonstrates this functionality.

Plugin Response[edit]

Summary[edit]

Plugin support is accomplished by triggering the onAjax[Name] plugin event.

Details[edit]

Plugin requests must include the plugin variable in the URL, paired with the name of the plugin event, e.g. plugin=session for onAjaxSession.

This value is also used for:

  • The plugin class name following the plgAjax[Name] convention.
  • The plugin function name following the onAjax[Name] convention.
  • The default plugin group is ajax. You can change it specifying the group request parameter. (From Joomla! 3.4)

The Ajax Latest Articles plugin is an example plugin that demonstrates this functionality.

Response Format[edit]

format=[json|debug] is an optional argument for the results format:

  • json for JSON format
  • debug for human-readable output of the results.