Solved

Ajax request onFailure

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

707 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

17 Experts available now in Live!

Get 1:1 Help Now