Solved

Ajax request onFailure

Posted on 2009-05-06
3
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

ASP.NET AJAX callbacks to web service are becoming very popular and unavoidable these days. The feel that the user gets when only a part of the page is updated without even hindering the UI is the advantage that the callbacks offer. The wait for the…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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