Forum:How to edit a page via jQuery $.ajax()?

Greetings and Good day! I hit a brick wall, and hope I can ask this unless such things are not allowed to begin with.

How do i edit a page via Ajax request? Here is a sample code of what 'I think' is what I'm doing right based on ghelo.wikia.com/api.php:

//** gets an edit token var ve = apiRequest('api.php?format=json&action=query&titles=Test&prop=info&intoken=edit').query.pages; //** filters the right editToken and the timeStamp for(var xa in ve)if(ve[xa].title=='Test'){ var ne = { token:ve[xa].edittoken,time:ve[xa].starttimestamp}; break; } //** remove necessary chars from timestamp ne.time = (+ne.time).replace(/\-/g,).replace(/\:/g,).replace(/\T/g,).replace(/\Z/g,'');

//** here goes nothing $.ajax({ type:'POST',async:false,url:'http://ghelo.wikia.com/api.php?action=edit&title=Test&summary=YES&basetimestamp='+ne.time+'&text=OnlyThisIShouldSee&token='+ne.token+'',success:function(done,a,b){

gheloBox( printProperties(b) ); // some functions? gheloBox( a );                 // String 'success' gheloBox( done );              // returns the '/api.php' page?

},error:function{

gheloBox("T^T"); // not happening

}}); ^ Note: gheloBox outputs a sort-of dialog box.

Now, no matter how I modify the URL and refreshing the Test page, the content is still the same. I'd understand that this may not be allowed due to possible vandalism and abuse, and if so, is there a way (or request a way) to get around it?

Hope to get an immediate response, this is such a big wall in the development of our wiki... T^T

Thank you in advance - ^^ Fe2+ | talk 17:15, December 19, 2011 (UTC)


 * I am completely not into Ajax and cannot answer that part.


 * But have you tried to purge the page using the same url while editing but instead of &action=edit you use &action=purge ?




 * Do you mean, In checking if the page was edited or not? Yup, I tried, i typed the url:  in the address bar. Hmm, or do you mean I should include this in the above code? - ^^ Fe2+ | talk 03:23, December 20, 2011 (UTC)


 * Firstly thank you Mr. WikiPim for the appreciating effort of helping me out. ^^ Also great thanks to Mr. Daniel Friesen for his code at w:c:dev:ArchiveTool/code.js that allowed me to study how to use  properly, you're name is credited at the top of the function name once the wiki is all pumped up - as due. ^^


 * I took the liberty to post the answer herein so it may be useful to any one who may encounter the same problem. ^^

Answer
function apiReq(q, fn) { q.format = 'json'; return $.ajax({ async: false, type: 'POST', url: ''+wgScriptPath + '/api.php', data: q, success: fn, dataType: 'json', }); }

function getToken(page, fn) { apiReq({ action: 'query',query: 'prop',prop: 'info',titles: page,intoken: 'edit' },function(q){  for( var k in q.query.pages )return fn(q.query.pages[k]); }); }

var p; getToken('Test',function(x){ p = x; });

apiReq({ action: 'edit', title: 'Test', text: 'YES!! Finally!!', token: p.edittoken, summary: "My FIRST API EDIT!!! \m/ ^^ \m/ YEH!!" + " " + wgPageName + ".", minor: true, },function(x){ gheloBoxTXT("OMG!\n\n"+printProperties(x) ); });

Hope, nobody minds me not efforting to remove my 'personal touches', I just wanna post this immediately before I go to bed before I forget. Thank you very much again community, really appreciate it - ^^ Fe2+ | talk 17:27, December 20, 2011 (UTC)


 * It's not needed to credit me anything, but the purge command is to force the server to renew the page which is used on the caching server. The caching server is in place to relieve the main server. It stores all recently used and/ or highly traffic pages so they can be very quickly shown as well as don't take up lots of bandwith from the main server.


 * It however has a downside of not showing the newest version if you are editing pages on occassion. Which can be easily overruled by the purge command.