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

Content deleted Content added
Updated with jQuery examples. See also mw:RL/JD#ajax.js and mw:RL/DM#jQuery & plugins
jQuery examples: fixing examples (to work on jQuery 1.4.4)
Line 13:
Fetching a page content can be done using [[GET]].
<source lang="javascript">
$.ajax( {
$.get('example.php', function(data) {
url: 'example.php',
alert('The remote page contains:\n' + data);
$.get('example.php', success: function( data ) {
})
alert( 'The remote page contains:\n' + data );
.error(function() {
},
alert('The query returned an error.');
error: function( ) {
});
alert( 'The query returned an error.' );
}
} );
</source>
===Edit a page and other common actions===
Line 25 ⟶ 28:
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].
<source lang="javascript">
$.getJSONajax({
url: mw.util.wikiScript( 'api' ), {
dataType: 'json',
data: {
format: 'json',
action: 'query',
Line 34 ⟶ 39:
titles: 'Whatever'
},
success: function( data ) {
var token = data.query.pages[ data.query.pageids[0] ].edittoken;
edit_page(token);
},
. error(: function() {
alert( 'The token query returned an error. =(' );
}
).error(function() {
alert( 'The token query returned an error. =(' );
});
 
Line 46 ⟶ 52:
var mySandbox = 'User:' + mw.config.get( 'wgUserName' ) + '/Sandbox';
 
$.postajax({
type: 'POST',
url: mw.util.wikiScript( 'api' ), {
data: {
action: 'edit',
title: mySandbox,
Line 54 ⟶ 62:
token: token
},
success: function() {
alert( 'Page edited!' );
},
). error(: function() {
alert( 'The edit query returned an error. =(' );
}
).error(function() {
alert( 'The edit query returned an error. =(' );
});
}
</source>
 
==Deprecated methods ==
===Create an AJAX object===