...

View Full Version : Ajax function that returns content of page?



H3X
01-03-2011, 07:44 AM
Hey, I am looking for an Ajax function that returns the contents of the page. I have been trying to use this:

function xmlhttpPost(strURL, cword) {
var xmlHttpReq = false;
var self = this;
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
return self.xmlHttpReq.responseText;
}
}
self.xmlHttpReq.send('q='+cword);
}
But, do you see on the 5th line up how I try to return the contents? This is obviously not going to work because the return is in another function.

So, basically, I just need a simple function that has POST capabilities, and returns the contents of the page. I have been trying to do this myself for the last hour or so with no luck. I also have been searching around quite a bit but can't find much.

Does anyone have one they could share, or could someone make the necessary adjustments to mine?

Thanks!

devnull69
01-03-2011, 08:35 AM
Returning something from an asynchronous callback function is not possible by design. Either you will have to process the result directly inside the callback or you will have to turn the asynchronous request into a synchronous one (which would enable you to return something).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum