Manual:Ajax

From MediaWiki.org

Jump to: navigation, search

MediaWiki offers an AJAX interface for use by extensions. It can be used as follows:

  • set $wgUseAjax = true; in LocalSettings.php (if your extension requires Ajax, make it check if $wgUseAjax is true, and if not, fail)
  • in your extension setup code, register a function for use via Ajax: $wgAjaxExportList[] = "myExtensionFunction"; The function's name may contain :: to reference a function defined inside a class.
    • The handler function you register will be called with the list of parameters passed to sajax_do_call (or rsargs[], respectively), see below.
    • The handler function you register can either return a string (which should be in the format the JavaScript code expects, typically HTML, XML, JSON, or a single textual value,) or, for more control over the HTTP headers of the response (especially with respect to the reported content type, cache control, etc) the function should return an instance of AjaxResponse.
  • in your extension's JavaScript code, you can now call your PHP function: sajax_do_call( "myExtensionFunction", [a, b] , callback ); a and b would be the two parameters that myExtensionFunction expects (you can have any number of parameters), and callback is a JavaScript function that takes an XMLHttpRequest as a parameter - this is used as the standard Ajax callback, that is, that function is called whenever the request's state changes.
    • sajax_do_call uses the action=ajax mode to call index.php. It passes the arguments using two additional parameters: rs (the function name, e.g., "myExtensionFunction"), and rsargs[] (for parameters, possibly repeated, e.g. rsargs[]=a&rsargs[]=b).

[edit] AjaxResponse

The AjaxResponse response class is defined in AjaxResponse.php. It represents the response sent by an Ajax handler function, and controls the actual response text as well as HTTP headers indicating the content type, cache control, etc.

Detailed documentation pending, see the source for now.

[edit] Examples

Some extensions that use MediaWiki's Ajax interface can be found in Category:Ajax extensions.

[edit] External Links

General information on XMLHttpRequest: