Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

AJAX: Difference between revisions

From MoodleDocs
(content copied from 19 wiki)
(Update some recommendations)
Line 1: Line 1:
'''AJAX (Asynchronous Javascript and XML)''' is a modern web design technique that allows for more interactivity by making webpages that fetch data in the background and alter themselves without reloading. This makes the website feel much more like an application. AJAX is a new way of working with existing technologies (including HTML, [[Javascript]], [[CSS]] and the ''XMLHttpRequest object'' amongst others) rather than a new piece of technology in itself.
'''AJAX (Asynchronous Javascript and XML)''' is a modern web design technique that allows for more interactivity by making webpages that fetch data in the background and alter themselves without reloading the entire page. This helps to make a page feel much more like an application than a web page. AJAX is a new way of working with existing technologies (including HTML, [[Javascript]], [[CSS]] and the ''XMLHttpRequest object'' amongst others) rather than a new piece of technology in itself.
 
Although AJAX indicates that XML is used, the term really relates to the group of technologies and in Moodle we tend to favour use of JSON rather than XML as the syntax is lighter and leads to a smaller output. It is also easier to construct from php data structures.


== Ajax in Moodle ==
== Ajax in Moodle ==


=== YUI framework ===
Moodle has been using the [[YUI]] framework for many years. YUI provides libraries for communicating data with the server, and for parsing the output received. The key parts of the YUI library which do this are:
 
* [http://yuilibrary.com/yui/docs/api/classes/IO.html Y.IO]
* [http://yuilibrary.com/yui/docs/api/classes/JSON.html Y.JSON] (''Note: native browser implementations are used it available. Y.JSON is a monkey-patch for older browsers lacking this support natively but it does provide a hint as to the relevant functions.'')
 
=== Writing server-side AJAX ===
 
For JavaScript to communicate with the server-side aspect of Moodle, you need to create server-side scripts written in PHP. These should use standard moodle APIs and libraries.
Any AJAX script in moodle '''must''' define the AJAX_SCRIPT variable before including moodle's configuration:
 
  define('AJAX_SCRIPT', true);
 
Doing so:


As of July 2006, Moodle has adopted the [[YUI|Yahoo! user interface library]] and most future work in this area will  build upon that foundation.
* ensures that any exceptions that your code may generate are returned in json notation rather than an HTML output (this means that you can parse them in your code and use the JS error notification dialogues which exist in Moodle);
* ensures that the correct document mime-type (application/json in most cases) is sent with the document (this can have important implications for some load balancers and reverse proxy implementations which attempt to inject additional content for certain mime-types); and
* ensures that AJAX versions of renderers are used (this is discussed further below).


There are several current projects that make use of these technologies e.g.:
All responses from your script should ideally be returned in JSON notation.


* [[Chameleon]] theme
''A proposal being considered at present is to suggest that AJAX versions of scripts should perhaps be named as NAME.ajax.php or NAME.json.php to make their use clearer at a glance''
* [[Student projects/AJAX course format]]


=== AJAX renderers ===
=== AJAX renderers ===


As of Moodle 2.0, AJAX output can be generated by creating an ajax renderer class named something like mod_forum_renderer_ajax in the same way and in the same file as the normal renderer. If a script is marked as being an AJAX script by having this line placed at the top of the file (before any other code)
Since Moodle 2.0, AJAX output can be generated by creating an ajax renderer class named something like mod_forum_renderer_ajax in the same way and in the same file as the normal renderer. If a script is marked as being an AJAX script by having this line placed at the top of the file (before any other code)


   define('AJAX_SCRIPT', true);
   define('AJAX_SCRIPT', true);
Line 24: Line 39:
* [[Javascript FAQ]]
* [[Javascript FAQ]]
* [[Javascript]]
* [[Javascript]]
* [[Unobtrusive javascript]]
* [[Hijax]]
* [[Hijax]]
* [[Firebug#Debugging_AJAX_with_Firebug|Debugging AJAX with Firebug]]
* [[Firebug#Debugging_AJAX_with_Firebug|Debugging AJAX with Firebug]]

Revision as of 23:00, 9 March 2013

AJAX (Asynchronous Javascript and XML) is a modern web design technique that allows for more interactivity by making webpages that fetch data in the background and alter themselves without reloading the entire page. This helps to make a page feel much more like an application than a web page. AJAX is a new way of working with existing technologies (including HTML, Javascript, CSS and the XMLHttpRequest object amongst others) rather than a new piece of technology in itself.

Although AJAX indicates that XML is used, the term really relates to the group of technologies and in Moodle we tend to favour use of JSON rather than XML as the syntax is lighter and leads to a smaller output. It is also easier to construct from php data structures.

Ajax in Moodle

Moodle has been using the YUI framework for many years. YUI provides libraries for communicating data with the server, and for parsing the output received. The key parts of the YUI library which do this are:

  • Y.IO
  • Y.JSON (Note: native browser implementations are used it available. Y.JSON is a monkey-patch for older browsers lacking this support natively but it does provide a hint as to the relevant functions.)

Writing server-side AJAX

For JavaScript to communicate with the server-side aspect of Moodle, you need to create server-side scripts written in PHP. These should use standard moodle APIs and libraries. Any AJAX script in moodle must define the AJAX_SCRIPT variable before including moodle's configuration:

 define('AJAX_SCRIPT', true);

Doing so:

  • ensures that any exceptions that your code may generate are returned in json notation rather than an HTML output (this means that you can parse them in your code and use the JS error notification dialogues which exist in Moodle);
  • ensures that the correct document mime-type (application/json in most cases) is sent with the document (this can have important implications for some load balancers and reverse proxy implementations which attempt to inject additional content for certain mime-types); and
  • ensures that AJAX versions of renderers are used (this is discussed further below).

All responses from your script should ideally be returned in JSON notation.

A proposal being considered at present is to suggest that AJAX versions of scripts should perhaps be named as NAME.ajax.php or NAME.json.php to make their use clearer at a glance

AJAX renderers

Since Moodle 2.0, AJAX output can be generated by creating an ajax renderer class named something like mod_forum_renderer_ajax in the same way and in the same file as the normal renderer. If a script is marked as being an AJAX script by having this line placed at the top of the file (before any other code)

 define('AJAX_SCRIPT', true);

then the AJAX renderer will be used in place of the main one for all components if it exists.

See also