Solved

Ajax request onFailure

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auto submit a form that launches a PHP scripts using Ajax/Javascript 8 41
Image Checkboxes 1 65
AJAX Call with two returns 19 76
javascript question 6 42
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now