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

    Made an AJAX Object, Doesn't Work

    Hi all, I wanted to learn AJAX so I have made a little object to make my life easier, unfortunately, It doesn't do anything at all... I'm sure I'm missing something


    Code:
    function Ajax ( Location , Method , RequestDone , Refresh)	{	
    	this.Conn = null;
    	this.Method = Method;
    	this.TimeLeft = '';
    	this.TimerID = '';
    	this.Data = '';
    	this.RequestDone = RequestDone;
    	
    	this.Handler = function ()	{
    		this.OnTimeOut();
    	};
    
    	try	{
    		// Modern Browsers - Firefox, Opera 8.0+, Safari
    		this.Conn=new XMLHttpRequest();
    	}
    	catch (e)	{
    		try	{
    			// New IEs
    			this.Conn = new ActiveXObject( "Msxml2.XMLHTTP" );
    		}
    		catch (e)	{
    			try	{
    				// Old IEs
    				this.Conn = new ActiveXObject( "Microsoft.XMLHTTP" );
    			}
    			catch (e)	{
    				alert( "Your browser does not support AJAX!" );
    				return false;
    			}
    		}
    	}
    	this.Conn.open( this.Method , this.Location , true );
    	this.Conn.onreadystatechange = function()	{
    		if( this.Conn.readyState == 4 )	{
    			if ( this.Conn.status == 200 )	{
    				var Response = '';
    				if ( this.Conn.responseText )	{
    					Response = this.Conn.responseText;
    				}
    				if ( this.Conn.responseXml )	{
    					Response = this.Conn.responseXml;
    				}
    				this.RequestDone( Response );
    			};
    		}
    	}
    	
    	this.SendRequest = function ( Data )	{	
    		this.Data = Data;
    		this.Conn.send ( Data , true );
    	};
    
    }
    Connection = new Ajax ( 'test.php' , "GET" , Handle);
    Connection.SendRequest();
    function Handle( Response )	{
    	alert ( Response );
    };
    I also have a Test page-
    Code:
    Connection = new Ajax ( 'test.php' , "GET" , Handle);
    Connection.SendRequest();
    function Handle( Response )	{
    	alert ( Response );
    };
    test.php writes "Hi!"

    thanks in advance
    Last edited by LithMaster; 11-10-2007 at 11:21 PM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by LithMaster View Post
    Code:
    function Ajax ( Location , Method , RequestDone , Refresh)	{	
    ...
    	this.Conn.open( this.Method , this.Location , true );
    ...
    }
    i'm not sure why commenting out that line new Ajax ( 'test.php' , "GET" , Handle) returns a valid object...u can try moving the init code to an init method perhaps
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    the line:
    Code:
    new Ajax ( 'test.php' , "GET" , Handle)
    should be:
    Code:
    new Ajax ( "GET", 'test.php', Handle)
    I think that should do it.


  •  

    Posting Permissions

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