Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    H3X
    H3X is offline
    New Coder
    Join Date
    Nov 2008
    Posts
    90
    Thanks
    30
    Thanked 0 Times in 0 Posts

    Ajax function that returns content of page?

    Hey, I am looking for an Ajax function that returns the contents of the page. I have been trying to use this:
    Code:
    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!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •