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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 07:23 AM. Reason: changing title

  • #2
    New Coder
    Join Date
    Mar 2007
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New to the CF scene
    Join Date
    May 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 02:46 PM.

  • #6
    New to the CF scene
    Join Date
    May 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

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