Gettting return values from Ajax.Request

I’m having some trouble trying to make a function using the prototype.js library. I want the function to return a value based on an Ajax.Request object.


function isLoggedIn(){
	var isLogged;
	url = "../script/isLoggedIn.cfm";
	xhr = new Ajax.Request(url,{method:'get', onComplete:function(){return ([I]text from the isLoggedIn.cfm page[/I])}});
	if ([I]text from page is true[/I]){return true;}else{return false;}

}

The problem I’m having is getting at the return value from the onComplete function while I’m outside of the function. I’m sure it’s something stupid that I’m missing, but I can’t find it.

The problem is that you are using an anonymous function for ‘onComplete:’ and this ends up limiting the scope of the return text to that function.

Try using something like ‘onComplete: myOnCompleteFunction()’ and then defining the function later:


function myOnCompleteFunction(text) {
     // Do whatever you want here.
     if (text from page is true){return true;}else{return false;} 
     return boolean;
}

If I wanted to get at the value from the myOnCompleteFunction while I was in the parent function (isLoggedIn()), how would I do that?

Well your isLoggedIn() function is starting an Ajax call, but that doesn’t mean it will get to complete it. Essentially I have a function that starts the call, and then separate functions that handle the possible return values. For instance:


function bounce() {
	$e('enter').disabled=true;
	var h = $e('horiz').value;
	var v = $e('vert').value;
	var g = $e('grav').value;
	var e = $e('ela').value;
	ptext("display","<img src=\\"images/loading.gif\\" alt=\\"loading..\\" />");
	$.ajax({type: "POST", url: "php/bounce.php", data: "v=" + v + "&h=" + h + "&g=" + g + "&e=" + e, success: printHTML, error: handleError});
	return;
}

function handleError (obj, input) {
	ptext("display","Experiencing a server error");
	return;
}

function printHTML(input) {
	$e('display').innerHTML = '<img src="' + input + '" alt="' + input + '" class="animated"/>';
	$e('enter').disabled = false;
	return;
}

// Hide a given element
function hide(el) {
	$e(el).style.display = "none";
	return;
}

// Show a given element
function show(el) {
	$e(el).style.display = "block";
	return;
}

// Update the innerHTML for an element
function ptext(el, t) {
	$e(el).innerHTML = t;
	return;
}

function $e(el) {
	return document.getElementById(el);
}

Here I’m using the jQuery JS library to handle the Ajax for me so I don’t have to learn what the various differences between the browsers are. So the $.ajax function handles the request back to the server, and you can see that two of the arguments for it are the name of a return function, and an error function (in case of a timeout or something similar).

Basically the script doesn’t just wait there for a reply from the server, it keeps moving along, so typically, an Ajax request should be the last line of a dummy function. Then once you have a return value from the server, you handle the return value in a separate function.

What this means for you is that you can’t access the return value in your isLoggedIn function, you need to handle that part of the logic (handling what happens with the user) in a different function.

Btw, this script is part of a script I wrote which takes some user entered values, and creates an animated Gif out of them using Php, and uses Ajax to send the information about the Gif to the server, then the server sends back the name of the image file it’s created, wrapped in an image HTML element.

Thanks for all your help. I did end up figuring out a way to get at that responseText though. The reason I couldn’t get it before was that the Ajax.Request call was going asynchronously. Once I switched that over, I was able to get at the variable after the Request call. Here’s the code:


function isLoggedIn(){
	url = "../script/isLoggedIn.cfm";
	xhr = new Ajax.Request(url,{[B]asynchronous:false[/B], method:'get', onComplete:null});
	if(trim(xhr.transport.responseText)== 'true'){
		return true;
	} else {
		return false;
	}
}

Thanks again for your help though