Solved

Ajax request onFailure

Posted on 2009-05-06
3
511 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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