Wikipedia:WikiProject User scripts/Guide/Ajax: Difference between revisions

Content deleted Content added
m Code: updated
m tweaks
Line 1:
[[Ajax (programming)|AJAX]] (''asynchronous JavaScript and XML'') is a popular name for a web programming technique that queries the server or fetches content without reloading the entire page.
 
While programming AJAX can be complex, libraries of functions can make it much easier. MediaWiki's [{{SERVER}}/skins-1.5/common/ajax.js ajax.js] provides a small set of functions by default, and [[m:User:Pathoschild/Scripts/Ajax framework|Pathoschild's AJAX framework]] provides a more comprehensive library of functions. This page will document bothprogramming librarieswith and without the latter.
 
==Common problems==
Line 25:
 
===Fetch page content===
Fetching a page content can easily be done using [[GET]].
<source lang="javascript">
/************
Line 40:
if(_api.readyState==4) {
if(_api.status==200)
alert('The remote page contains:\n' + api_api.responseText);
else
alert('The query returned an error.');
Line 52:
function show_result(_api) {
if(_api.success)
alert('The remote page contains:\n' + api_api.responseText);
else
alert('An error occurred.');
Line 59:
 
===Edit a page and other common actions===
Scripts can easily perform common actions (like editing, protection, blocking, deletion, etc) through the [{{SERVER}}/w/api.php API]. These actions require an edit token, which is valid for any action during the same session. (However, you should get a new token for different tasks in case this changes in the future.)
 
The code below shows how to edit a page, but it can easily be adapted to other actions by reading the [{{SERVER}}/w/api.php API documentation].