Solved

Ajax request onFailure

Posted on 2009-05-06
3
494 Views
Last Modified: 2012-05-06
Hi there, i am using Ajax via the Prototype Framework.
I would like to be able to monitor whether i get a response or not from  request, within a certain time.
Using firebug, i can see that sometimes, the response does not come back(request indicated with red color on firebug), or takes too long to come back. On both those cases i would like to know so that i can do something about it.
I have set the onFailure callback but it never got triggered.

What can i do?
0
Comment
Question by:ioustinos_sarris
  • 2
3 Comments
 
LVL 4

Expert Comment

by:aconrad
ID: 24321981
i found the timeout option for ajax a pretty cool thing so i did it in a pure javascript ..
maybe you can use it ...

The php scripts intentionally waits 5 seconds until it finishes.
javascript function waitforajax(seconds) is selfdescribing :)

<?
if (isset($_GET['ajax'])) {
	sleep(5);
	die();
}
 
 
?>
<a href="#" onclick="waitforajax(2);">testing timeout (waits 2 seconds)</a><Br>
<a href="#" onclick="waitforajax(6);">testing normal (waits 6 seconds)</a><Br>
 
 
<div id="started"></div>
<div id="finished"></div>
<div id="timeout"></div>
 
<script>
 
var started='';
var wait=0;
 
function doTime() {
 
 
 
	var currentDate = new Date();
	return parseFloat(currentDate.getTime())/1000;
}
 
 
function waitforajax(seconds) {
	document.getElementById('started').innerHTML = '';
	document.getElementById('finished').innerHTML = '';
	document.getElementById('timeout').innerHTML = '';
 
	started = doTime();
	wait = seconds;
	document.getElementById('started').innerHTML =  'AJAX Request started at :'+started;
 
	ajax_request('testajax.php?ajax=1','GET','','checkresponse');
}
 
function checkresponse()
{
 
	var now;
 
	now = doTime();
 
	if ((parseFloat(now)-parseFloat(started)) > wait)
	{
		document.getElementById('timeout').innerHTML =  'Timeouted after '+wait+'seconds!';
	}
	else
	{
 
 
		var iserr = false;
		if (xmlhttp.readyState==4)
		{
			if (xmlhttp.status==200)
			{
				document.getElementById('finished').innerHTML = 'AJAX Request finished at '+now+' with http code '+ xmlhttp.status;
				var resp = xmlhttp.responseText;
 
			}
			else
			{
				alert("Problem retrieving data:" + xmlhttp.statusText)
			}
		}
		else
		{
 
		}
	}
}
 
 
 
 
/**
* XML HTTP Request. Works with in cinjuction with the specified myfunc function which checks if the request was succesfull
*
* @param string $url
* @param POST-or-GET $method
* @param urlencoded-string $data
* @param string $myfunc
*/
function ajax_request(url,method,data,myfunc)
{
 
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest()
		if (method=="GET")
		{
			xmlhttp.onreadystatechange=eval(myfunc)
			xmlhttp.open("GET",url,true)
			xmlhttp.send(null)
		}
		if (method=="POST")
		{
			xmlhttp.onreadystatechange=eval(myfunc)
			xmlhttp.open("POST", url, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlhttp.send(data)
		}
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp)
		{
			if (method=="GET")
			{
				xmlhttp.onreadystatechange=eval(myfunc)
				xmlhttp.open("GET",url,true)
				xmlhttp.send()
			}
			if (method=="POST")
			{
				xmlhttp.onreadystatechange=eval(myfunc)
				xmlhttp.open("POST", url, true);
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlhttp.send(data)
			}
		}
	}
}
 
 
</script>

Open in new window

0
 

Author Comment

by:ioustinos_sarris
ID: 24323353
As far as i understand with this script you check how much time has passed since you sent the request. This is usefull indeed.
But how can you check whether or not you got the reponse or not?
My main problem is with calls that respond with 302 or with calls which dont respond at all (indicated with red on firebug console).
Moreover i would not like to change my code from the Prototype Framework as i am using the onLoading, onSuccess and onUnitialiazed functions.

thanks
0
 
LVL 4

Accepted Solution

by:
aconrad earned 500 total points
ID: 24324293
in the line that displays the timeout (line 52),
the below variables are present and can be used to get info about the request :

xmlhttp.status   -- http code
xmlhttp.responseText   -- partial response
xmlhttp.readyState -- this 0,1,2,3,4 and it means:
0      The request is not initialized
1      The request has been set up
2      The request has been sent
3      The request is in process
4      The request is complete

for ex:
document.getElementById('timeout').innerHTML =  'Timeouted after '+wait+'seconds! Code: '+xmlhttp.status;


More info / examples of your firebug problem would help to understand what can be done....
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question