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

Content deleted Content added
Edit a page and other common actions: XHR.send requires parameters, should be null if there are none
Edit a page and other common actions: bugfix, wgScriptPath instead of hardcoded path
Line 69:
// fetch token
var api = sajax_init_object();
api.open('GET', wgServer + wgScriptPath + '/w/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=edit&titles=Whatever', true);
api.onreadystatechange = extract_token;
api.send(null);
Line 86:
 
// edit page (must be done through POST)
function edit_tokenedit_page(_token) {
var parameters = 'action=edit&title=User:Pathoschild/Sandbox&text=AJAX_test!&token=' + encodeURIComponent(_token);
api.open('POST', wgServer + wgScriptPath + '/w/api.php', true); // just reuse the same query object
api.onreadystatechange = alert_result;
api.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
Line 114:
function edit_page(_token) {
var parameters = 'action=edit&title=User:Pathoschild/Sandbox&text=AJAX_test!&token=' + encodeURIComponent(_token);
ajax_post(wgServer + wgScriptPath + '/w/api.php', parameters, alert_result);
}
function alert_result(_api) {