Important alert: (current site time 7/25/2014 9:21:17 AM EDT)
 

VB icon

AJAX Class

Email
Submitted on: 7/25/2011 6:18:55 PM
By: Christopher J McIntosh  
Level: Intermediate
User Rating: Unrated
Compatibility: JavaScript
Views: 3332
author picture
(About the author)
 
     This class creates a browser independent xmlhttprequest object and allows the developer to POST and GET back-end data and do a number of things with the response. Supports JSON. Fully documented and extend able.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: AJAX Class
' Description:This class creates a browser independent xmlhttprequest object and allows the developer to POST and GET back-end data and do a number of things with the response. Supports JSON. Fully documented and extend able.
' By: Christopher J McIntosh
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6712&lngWId=14'for details.'**************************************

/*
Author: Christopher J. McIntosh
*/
function Ajax()
{
	//var to hold the page to request
	this.sUrl 	= new String();
	//var to hold the request method
	this.sMethod 	= new String("GET");
	//flag to use asynchronouse or synchronouse
	this.bAsync 	= new Boolean(true);
	//method to handle the response from the XHR
	this.fCallBack 	= null;
	//boolean value to let us know if returned text is JSON
	this.bJSON 	= new Boolean(false);
	//method to handle the returned JSON response
	this.fJSON 	= null;
	//DHTML object that will hold the returned text
	this.oContainer = null;
	//string that will hold the URI encoded params from aParams
	this.sEncoded 	= new String();
	//array to hold the params to be sent to request document
	//params are added via prototype method setParam
	this.aParams 	= new Array();
	//ready state functions
	this.fLoading 	= null;
	this.fLoaded	= null;
	this.fInteract	= null;
	//cross browser compatible XMLHttpRequest object
	this.oXHR 	= Ajax.XHR();
}
//Class Method
Ajax.XHR = function()
{
	oXHR = false;
	try
	{
oXHR = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			oXHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e2)
		{
oXHR = false;
		}
	}
	if(!oXHR && typeof XMLHttpRequest != "undefined")
	{
		try
		{
oXHR = new XMLHttpRequest();
		}
		catch(e)
		{
oXHR = false;
		}
	}
	if(!oXHR && window.createRequest)
	{
		try
		{
oXHR = window.createRequest();
		}
		catch(e)
		{
oXHR = false;
		}
	}
	return oXHR;
}
Ajax.PHPJSON = function(o)
{	
}
//Instance Methods
Ajax.prototype.transmit = function()
{
	var self = this;
	this.encodeParams();
	if(this.sMethod == "GET")
	{
sUrl = this.sUrl + "?" + this.sEncoded;
this.oXHR.open("GET", sUrl, this.bAsync);
	}
	else
	{
this.oXHR.open("POST", this.sUrl, this.bAsync);
this.oXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//this.oXHR.setRequestHeader("Connection", "close");
	}
	this.oXHR.onreadystatechange = function()
	{
		nState = self.oXHR.readyState;
		switch(nState)
		{
			case 1:
			if(self.fLoading){self.fLoading();}
			break;
			case 2:
			if(self.fLoaded){self.fLoaded();}
			break;
			case 3:
			if(self.fInteract){self.fInteract();}
			break;
			case 4:
				if(self.fCallBack)
				{
var sResponse = new String(self.oXHR.responseText);
sResponse = sResponse.replace(/\<\![\s\S]*$/gi, "");
self.fCallBack(sResponse, self.oXHR.responseXML);
				}
				if(self.oContainer)
				{
					self.oContainer.innerHTML = self.oXHR.responseText;
				}
				if(self.bJSON)
				{
					//alert(self.oXHR.responseText);
// var reg = /[h]/gi;
// alert(reg);
var sResponse = new String(self.oXHR.responseText);
sResponse = sResponse.replace(/\<\![\s\S]*$/gi, "");
					oJSON = eval("(" + sResponse + ")");
					self.fJSON(oJSON);
				}
			break;
		}
	}
	this.oXHR.send(this.sEncoded);
}
Ajax.prototype.setParam = function(sKey, sValue)
{
this.aParams[sKey] = sValue;
}
Ajax.prototype.encodeParams = function()
{
	for(e in this.aParams)
	{
		this.sEncoded += e + "=" + encodeURIComponent(this.aParams[e]) + "&";
	}
	//this.sEncoded = encodeURI(this.sEncoded);
}
/*EXAMPLE USAGE*/
//create AJAX instance
	oAjax					= new Ajax();
//choose request method
	oAjax.sMethod			= "POST";
//set backend document to request
	oAjax.sUrl				= "path/to/backend/page.php";
//tell the class that the response is in JSON format
	oAjax.bJSON				= true;
//keep a refference to the parent object for reffering to the ajax instance from within
// hte JSON callback
	var self 				= this;
//create inline function to handle the JSON object
	oAjax.fJSON		= 	function(oJSON)
								{
 //PROCESS JSON Object
								}
//SET PARAMETERS to send to the backend page.
	oAjax.setParam("name", "chris");
oAjax.setParam("age",33);
oAjax.setParam("skills", "PHP, AJAX, CSS");
//SEND THE REQUEST
	oAjax.transmit();


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.