Universal AJAX function

Here is my universal AJAX function. I am posting it here for reference. Over the months I have adapted/revised this function and now have finally gotten it to do pretty much what I want it. I did not write all of this myself I pulled most of it off of somewhere, but I can not remember so I can not give proper credit. Let me know if you have any suggestions on how to improve it.

 

// ******************************************************
// Universal Ajax Function
// Useage Example:  universalAJAX('http://url.com', "var1=true&var2=false", functionWhenDone, functionWhenWaiting, functionWhenFail);
// ******************************************************

function universalAJAX(URL, DataToSend, doneFunction, waitingFunction, errorFunction)
{

var json_http;
if (window.XMLHttpRequest)
{json_http=new XMLHttpRequest();}
else
{json_http=new ActiveXObject("Microsoft.XMLHTTP");}

json_http.onreadystatechange=function()
 {
   if (json_http.readyState==1)
    {waitingFunction();}
  if (json_http.readyState==4 && json_http.status==200)
    {
	if (json_http.status==200)
		{
		doneFunction(json_http.responseText);
		clearTimeout(timeOutVarCancel);
		}
	else
		{
		errorFunction();
		clearTimeout(timeOutVarCancel);
		}
	}
}

  json_http.open("POST",URL,true);
  json_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  json_http.send(DataToSend);

  var timeOutVarCancel=setTimeout(errorFunction,10000);
}
  1. Jaysen says:

    Really a good idea

  1. There are no trackbacks for this post yet.