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

Content deleted Content added
jQuery examples: fixing examples (to work on jQuery 1.4.4)
corrections
Line 13:
Fetching a page content can be done using [[GET]].
<source lang="javascript">
$.ajax( {
url: 'example.php',
success: function( data ) {
alert( 'The remote page contains:\n' + data );
},
error: function( ) {
alert( 'The query returned an error.' );
}
} );
</source>
===Edit a page and other common actions===
Line 35:
action: 'query',
prop: 'info',
indexpageids: '1'true,
intoken: 'edit',
titles: 'Whatever'
Line 41:
success: function( data ) {
var token = data.query.pages[ data.query.pageids[0] ].edittoken;
edit_pagedoEditPage( token );
},
error: function() {
Line 49:
 
// Edit page (must be done through POST)
function edit_pagedoEditPage( token) {
var mySandbox = 'User:' + mw.config.get( 'wgUserName' ) + '/Sandbox';
 
$.ajax({
type: 'POST',
url: mw.util.wikiScript( 'api' ),
type: 'POST',
dataType: 'json',
data: {
format: 'json',
action: 'edit',
title: mySandbox,
Line 62 ⟶ 64:
token: token
},
success: function( data ) {
if ( data && data.result && data.result == 'Success' ) {
alert( 'Page edited!' );
} else {
alert( 'The edit query returned an error. =(' );
}
},
error: function() {
alert( 'The editajax queryrequest returned an errorfailed. =(' );
}
});