Manual:Ajax

From MediaWiki.org
Jump to: navigation, search

MediaWiki offers an AJAX interface for use by extensions. AJAX is a term for using JavaScript to load parts of a page on demand. It can be used as follows:

  • set $wgUseAjax = true; in LocalSettings.php, it is set to true by default in MediaWiki 1.17 & above (note that if your extension requires Ajax, make it check if $wgUseAjax is true, and if not, fail)
  • MediaWiki 1.16 is shipped with jQuery version 1.3.2, accordingly there is no need to load any separate library for Ajax

Contents

[edit] Examples

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

[edit] Add to your resource file

Below is an example call to the login API by posting the username & password

$.ajax({
        type: "GET", // request type ( GET or POST )
        url: mw.util.wikiScript('api'), // the URL to which the request is sent           
        data: { 'action':'query', 'format':'json', 'lgname':'foo', 'lgpassword':'foobar' }, // data to be sent to the server
        dataType: 'json', // the type of data that you're expecting back from the server
        success: function( jsondata ){ //function to be called if the request succeeds
                alert( jsondata.result );
        }
});

The function "mw.util.wikiScript" is available since 1.18 onwards.

[edit] See also

[edit] External links

General information on XMLHttpRequest:

Language: English  • 日本語
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox