AJAX woes

Discussion in 'JavaScript' started by ryepdx, Jan 19, 2008.

  1. #1
    I'm having trouble implementing an AJAX-driven "one-liner" board. For some reason when the user tries to post something to the board, the page hangs. My debugging alerts aren't even firing. I can't figure out why this is happening. Can someone take a look at this?
    Thanks.

    The code for the script is below. When the user clicks a button, "write()" fires.
    If you want to see the script in its natural habitat, go to http://friendblastr.com/sandbox/ and log in with the ID 201921563
    Much thanks.

    
    var padder = getXmlHTTP();
    var date = "";
    
    function write()
    {
    	alert("Click");
    	document.getElementById("writeButton").value="Writing...";
    	
    	if(padder.readyState==0)
    	{
    		alert("Requesting.");
    		line = document.getElementById("line").value;
    		padder.open("POST", "write.php5", true); 
    		padder.onreadystatechange = writing;
    		padder.send("line="+encodeURIComponent(line));
    	}
    	else
    		setTimeout('write()', 1000);
    		
    }
    
    function writing()
    {
    	alert("State:" + padder.readyState);
    	if(padder.readyState==4)
    	{
    		alert("Got response.");
    		read();
    		document.getElementById("line").value = "";
    		document.getElementById("writeButton").value="Write it!";
    		padder = getXmlHTTP();
    	}
    }
    
    function read()
    {
    	if(padder.readyState==0)
    	{
    		padder.open("POST", "read.php5", true);
    		padder.onreadystatechange = reading;
    		padder.send("date="+encodeURIComponent(date));
    	}
    	else
    		setTimeout('read()', 1000);
    }
    
    function reading()
    {
    	if(padder.readyState==4)
    	{
    		lines = padder.responseText.split("\n");
    		
    		for (i = 0; i < lines.length-1; i++)
    			document.getElementById("pad").innerHTML += lines[i];
    			
    		date = lines[lines.length-1];
    		padder = getXmlHTTP();
    	}
    }
    
    function getXmlHTTP()
    {
    	var obj;
    	
    	try
    	{
    		obj = new XMLHttpRequest();
    	}
    	catch (e)
    	{
    		try
    		{
    			obj = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			try
    			{
    				obj = new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e)
    			{
    				alert("This website will not work for you because your browser does not support AJAX.\nPlease update your browser (Firefox is recommended) and try again.");
    			}
    		}
    	}
    	return obj;
    }
    
    read();
    
    Code (markup):
     
    ryepdx, Jan 19, 2008 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
  3. ryepdx

    ryepdx Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I checked out your code and modified mine accordingly, but it's still doing the same thing. :-/
    The thing that really bugs me is that it's not even executing my "alert" statements. I have one as the first statement in the onClick handler and even THAT isn't going off!

    var padder = getXmlHTTP();
    
    var date = "";
    
    
    
    function write()
    
    {
    
    	alert("writing...");
    
    	document.getElementById("writeButton").value="Writing...";
    
    	
    
    	line = document.getElementById("line").value;
    
    	padder.open("POST", "write.php5", true);
    
    	padder.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    
    	padder.onreadystatechange = writing;
    
    	padder.send("line="+encodeURIComponent(line));
    
    }
    
    
    
    function writing()
    
    {
    
    	if(padder.readyState==4)
    
    	{
    
    		if (padder.status==200)
    
    		{
    
    			padder = getXmlHTTP();
    
    			read();
    
    			document.getElementById("line").value = "";
    
    			document.getElementById("writeButton").value="Write it!";
    
    		}
    
    		else
    
    		{
    
    			alert("Error writing! Try again.")
    
    			document.getElementById("writeButton").value="Write it!";
    
    		}
    
    	}
    
    }
    
    
    
    function read()
    
    {
    
    	padder.open("POST", "read.php5", true);
    
    	padder.onreadystatechange = reading;
    
    	padder.send("date="+encodeURIComponent(date));
    
    }
    
    
    
    function reading()
    
    {
    
    	if(padder.readyState==4)
    
    	{
    
    		lines = padder.responseText.split("\n");
    
    		
    
    		for (i = 0; i < lines.length-1; i++)
    
    			document.getElementById("pad").innerHTML += lines[i];
    
    			
    
    		date = lines[lines.length-1];
    
    		padder = getXmlHTTP();
    
    	}
    
    }
    
    
    
    function getXmlHTTP()
    
    {
    
    	var obj;
    
    	
    
    	try
    
    	{
    
    		obj = new XMLHttpRequest();
    
    	}
    
    	catch (e)
    
    	{
    
    		try
    
    		{
    
    			obj = new ActiveXObject("Msxml2.XMLHTTP");
    
    		}
    
    		catch (e)
    
    		{
    
    			try
    
    			{
    
    				obj = new ActiveXObject("Microsoft.XMLHTTP");
    
    			}
    
    			catch (e)
    
    			{
    
    				alert("This website will not work for you because your browser does not support AJAX.\nPlease update your browser (Firefox is recommended) and try again.");
    
    			}
    
    		}
    
    	}
    
    	return obj;
    
    }
    
    
    
    read();
    
    
    Code (markup):
    Thanks.
     
    ryepdx, Jan 19, 2008 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That looks nothing like my code. I'd also change the function names. "write" is a JavaScript reserved word.
     
    Mike H., Jan 19, 2008 IP
  5. ryepdx

    ryepdx Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah... I just tried to take the essential differences between your code and mine and incorporate them into my code. And yeah, probably should change "write" to "post" or something...

    I'll try coying your code and just modifying it a little and see how that works.

    Thanks.
     
    ryepdx, Jan 19, 2008 IP
  6. ryepdx

    ryepdx Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Alright, thanks. I got it working. :)
     
    ryepdx, Jan 19, 2008 IP
  7. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You're welcome.
     
    Mike H., Jan 20, 2008 IP