Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-14-2007, 05:03 AM   PM User | #1
ecgbert
New to the CF scene

 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
ecgbert is an unknown quantity at this point
Problem: Passing the XMLhttprequest object to the onreadystatechange function

I mean the onreadystatechange, not "onRequestChange"

Ok. I have a script that's intended to let a user login with his username and then get a list of all the users (and other things too, but I'll eliminate them for clarity.

Here's how it works.

Code:
//This first function just creates the object.
function createRequest() {

var newrequest;
try{
		newrequest = new XMLHttpRequest();
	} catch (e){
		try{
			newrequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				newrequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("You browser pro\'lly don\'t support AJAX, get the newest version of Firefox");
				return false;
			}
         	}
	}
return newrequest;
}
//This next function is one I use as a multipurpose request sending thing.
function sendRequest(action)
{
switch (action)  {
  case "login":
var destination = "login.php";
var parameters = "username=" + username + "&action=login";
var handlereturn = new handleLogin();
break;
}
var request = createRequest();
request.onreadystatechange = handlereturn();
request.open("POST", desination, true);

request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

request.setRequestHeader("Content-length", parameters.length);

request.setRequestHeader("Connection", "close");

request.send(parameters); 
}

function handleLogin() {
if (request.readyState == 4) {
switch (request.responseText) {
case "Logged In":
//Log In Successful
username = document.getElementById('username').value;
break;
case "Already Logged":
alert ("You or somebody else with that username is already logged in.");
break;
}
}
}
The problem is whenever I get to the handleLogin function, I get the error

"request is not defined".

I've tried making it global, and passing it as an argument but nothing seems to work. What am I doing wrong?

Thanks for your time!

Last edited by ecgbert; 05-14-2007 at 06:23 AM.. Reason: changing title
ecgbert is offline   Reply With Quote
Old 05-14-2007, 09:08 AM   PM User | #2
snake_eyes
New Coder

 
Join Date: Mar 2007
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
snake_eyes is an unknown quantity at this point
Smile

Hello,

Try to use the following code:

Code:
//This first function just creates the object.
function createRequest()
{
    var newrequest = false;
	try
	{
		newrequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			newrequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				newrequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("You browser pro\'lly don\'t support AJAX, get the newest version of Firefox");
				return false;
			}
	    }
	}
}
//This next function is one I use as a multipurpose request sending thing.
function sendRequest(sRequest,action)
{
	switch (action)
	{
  		case "login":
		var destination = "login.php";
		var parameters = "username=" + username + "&action=login";
		var handlereturn = new handleLogin();
		break;
	}
	sRequest.onreadystatechange = function { handlereturn() };
	sRequest.open("POST", desination, true);
	sRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	sRequest.setRequestHeader("Content-length", parameters.length);
	sRequest.setRequestHeader("Connection", "close");
	sRequest.send(parameters);
}

function handleLogin(sRequest)
{
	if (sRequest.readyState == 4)
	{
		switch (sRequest.responseText)
		{
			case "Logged In":
				//Log In Successful
				username = document.getElementById('username').value;
				break;
			case "Already Logged":
				alert ("You or somebody else with that username is already logged in.");
				break;
		}
	}
}
Please note: don't forget to pass newrequest into SendRequest function

Regards
snake_eyes is offline   Reply With Quote
Old 05-14-2007, 09:15 AM   PM User | #3
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Code:
request.onreadystatechange = handlereturn();
Means that you are assigning the result of handlereturn function to be the onreadystatechange handler. Which is not what you intended to do

Code:
request.onreadystatechange = handlereturn;
Is the right way to do it. If you want the XMLHttpRequest object to be passed as a parameter to the handlereturn function when it is called you create a closure like this

Code:
request.onreadystatechange = function() {
  handlereturn(request);
};
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 05-14-2007, 09:21 AM   PM User | #4
snake_eyes
New Coder

 
Join Date: Mar 2007
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
snake_eyes is an unknown quantity at this point
all are the same, but I'm using this way to pass the object into functions,

Is my code is working or not? waiting your feedback in order to modify it to work properly.

Regrds
snake_eyes is offline   Reply With Quote
Old 05-14-2007, 01:34 PM   PM User | #5
ecgbert
New to the CF scene

 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
ecgbert is an unknown quantity at this point
SnakeEyes: I gotta run right now, but thanks and I'll try your code.

Shyam: So I have to use an anonymous function for the on ReadyState changes? That's quite irritating, but alright. I was using anonymous functions in my code earlier and it worked fine for a while, but then I started to have my same problem. I decided just to start over and make my code better and use my switch/case blocks.

Last night I figured out that I didn't want the parenthesis or the "new", so I fixed it but still had problems.

Thanks again!

Last edited by ecgbert; 05-14-2007 at 01:46 PM..
ecgbert is offline   Reply With Quote
Old 05-14-2007, 10:49 PM   PM User | #6
ecgbert
New to the CF scene

 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
ecgbert is an unknown quantity at this point
Thank you Shyam. I took your advice and now it works like a charm.

Thanks for trying to help, snake_eyes, but I don't think that's what I need to do since I'm sort of copying the createrequest function thing right in my function, but thanks anyway!
ecgbert is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:42 PM.


Advertisement
Log in to turn off these ads.