ajax readyState problem

ajax readyState goes to 4 more than once, second responseText is blank

Page 1 of 1

1 Replies - 2083 Views - Last Post: 05 August 2009 - 04:29 AM

#1 aosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-July 09

ajax readyState problem

Post icon  Posted 23 July 2009 - 05:24 PM

I'm stumped on this one...

I'm having an issues with this script from my own debugging it appears that the readyState is doing the following
1 ... 4 ... 1 ... 4
the second time it appears not to have any response so I end up seeing my content shortly (if all my debugging alerts are on) but it then dissapears when the ready state jumps to 4 for the second time.

	//Begin AJAX
	//Browser Support Code
	function goToPage(loc)
	{
	   var ajaxRequest;
	   try
	   {
		  // Opera 8.0+, Firefox, Safari
		  ajaxRequest = new XMLHttpRequest();
		  alert("ajaxRequest created!");
	   }
	   catch (e)
	   {
		  // Internet Explorer Browsers
		  try
		  {
			 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		  }
		  catch (e)
		  {
			 try
			 {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			 }
			 catch (e)
			 {
				// Something went wrong
				alert("Your browser broke!");
				return false;
			 }
		  }
	   }
	   // Create a function that will receive data sent from the server
	   ajaxRequest.onreadystatechange = function(loc)
	   {
		  if(ajaxRequest.readyState == 4)
		  {
			 if(ajaxRequest.status == 200 || ajaxRequest.status == 304)
			 {
				document.getElementById('greenBox').innerHTML = ajaxRequest.responseText;
				
			 }
		  }
	   }
	   //contruct url
	   var url = "pageserver.php?loc=" + loc
	   ajaxRequest.open("GET", url, true);
	   ajaxRequest.send(null);
	}



pageserver.php:
	<?php

	//Begin frame
	/*
	if ($_GET['loc'] == "")
	{
	   $textRight = "";
	   $textLeft = "";
	   echo encap($textRight, $textLeft);
	}
	*/
	require_once("inc/dbinfo.inc");

	if ($_GET['loc'] == "home")
	{
	   $textRight = '##############################################';
	   $textLeft = '###############################################';
	   echo encap($textLeft, $textRight);
	}

	if ($_GET['loc'] == "register")
	{
	   $textRight = '##############';
	   $textLeft = '################';
	   echo encap($textLeft, $textRight);
	}

	if ($_GET['loc'] == "browse")
	{
	   $query = "######################";
	   $result = mysql_query($query);
	   for($i=0; $i > 10; $i++)
	   {
		  $array = mysql_fetch_assoc($result);
		  if ($i == 0)
		  {
			 $textRight = #########################;
			 $textLeft = ######################;
		  }
		  else if ($i > 0)
		  {
			 $textRight = #############################################;
			 $textLeft = ##############################################;
		  }
	   echo encap($textLeft, $textRight);
	   }
	}

	if ($_GET['loc'] == "about")
	{
	   $textRight = '############################';
	   $textLeft = '#############################';
	   echo encap($textLeft, $textRight);
	}

	if ($_GET['loc'] == "bid")
	{
	   if (!$_GET['id'])
	   {
		  //error
	   }
	   else if ($_GET['id'])
	   {
		  $id = $_GET['id'];
		  $query = "#########################";
		  $result = mysql_query($query);
		  $array = mysql_fetch_assoc($result);
	   }
	   
	   echo encap($textLeft, $textRight);
	}
	function encap($textLeft, $textRight)
	{
	   return ('<span id="leftBox" class="leftBox">'.$textLeft.'</span><span id="rightBox" class="rightBox">'.$textRight.'</span>');
	}
	?>



any ideas? tia

Is This A Good Question/Topic? 0
  • +

Replies To: ajax readyState problem

#2 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: ajax readyState problem

Posted 05 August 2009 - 04:29 AM

This is my last AJAX function. ( Edited ) Might help you.

function sendAJAXrequest( param1, param2 )
{

	function readyStateUpdate()
	{
		if ( ajaxRequest.readyState != 4 )
		{
			// show circular ajax flower somewhere, or a text that says "Loading/Updating data..."
		}
		else
		{
			var responseData = ajaxRequest.responseText;
			
		}
	}

	xmlHttp.open( "POST", "ajax/ajaxRequest.php", true );
	var params = "loc=" + loc;
	xmlHttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = readyStateUpdate;
	xmlHttp.send( params );
}



The difference is:
1) You put your whole ajaxRequest separately from creating of XMLHttpObject.
2) Before send request line, you put "ajaxRequest.onreadystatechange = readyStateUpdate;", which calls "readyStateUpdate();" function for manipulating ajax answers
3) Read all my function, replace with your variables, paths and names and It will work fine. Or, ICQ me 199128769 for help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1