Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads
 
Add your own
alternative version

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

, 22 Jan 2013 CPOL
An article about how to keep AJAX simple as it is and get the most out of it.
ajaxiontest1.0_java_src.zip
AjaxionTest
ajaxionTest.jpr
ajaxionTest.jpr.bak
AjaxionTest.jws
classes
ajaxionTest.cdi
pw
ajaxion
AjaxionEventConsumer.class
log
FileLog.class
servlet
ajaxionTest.class
servlet.cdi
public_html
css
images
processing.gif
ready.jpg
js
WEB-INF
pw.ajaxion
classes
pw
ajaxion
AjaxionEventConsumer.class
AjaxionEventHandler.class
pw.ajaxion.cdi
pw.ajaxion.jpr
src
pw
ajaxion
pw.log
classes
audit.log
pw
log
FileLog.class
pw.log.cdi
test
pw
log
FileLogTest.class
pw.log.jpr
src
pw
log
test
pw
log
src
servlet
ajaxiontest1.0_net1.1_src.zip
Ajaxion
Ajaxion.csproj.user
Ajaxion.ico
js
AjaxionTest.csproj.webinfo
Audit
Audit.csproj.user
Audit.ico
css
Global.asax
images
AjaxFlavour1.gif
AjaxFlavour2.gif
AjaxFlavour3.jpg
App.ico
HappyAccountant.jpg
processing.gif
ready.JPG
js
ajaxiontest1.0_net2.0_src.zip
Ajaxion.csproj.user
Ajaxion.ico
App_Code
Audit.csproj.user
Audit.ico
Global.asax
AjaxFlavour1.gif
AjaxFlavour2.gif
AjaxFlavour3.jpg
App.ico
HappyAccountant.jpg
processing.gif
ready.JPG
function Ajaxion(defaultUrl)
{
	// Members
	var request = null;
	var currentEventId = null;
	var allowedEventIdsCsv = null;
	var param = null;

	// Expose Methods
	this.post = post;
	this.postUrl = postUrl;
	this.get = get;
	this.getUrl = getUrl;
	this.parseEventParam = parseEventParam;
	this.showStatus = showStatus;
	this.showStatusImg = showStatusImg;
	this.setEventMonior = setEventMonior;
	this.getParam = getParam;
	this.getResponseString = getResponseString;
	this.getResponseXml = getResponseXml;
	this.getRequest = getRequest;
	this.isEventConsumed = isEventConsumed;
	this.beginEvent = beginEvent;
	this.endEvent = endEvent;

	// Exposed Methods

	function post(eventId, callbackConsumer)
	{
		callUrl("POST", defaultUrl, eventId, callbackConsumer);
	}
	function postUrl(url, eventId, callbackConsumer)
	{
		callUrl("POST", url, eventId, callbackConsumer);
	}

	function get(eventId, callbackConsumer)
	{
		callUrl("GET", defaultUrl, eventId, callbackConsumer);
	}
	function getUrl(url, eventId, callbackConsumer)
	{
		callUrl("GET", url, eventId, callbackConsumer);
	}

	function parseEventParam(eventId)
	{
		return null;
	}
	function getParam()
	{
		if (param != null)
			return param;
		else
			return "";
	}
	function getRequest()
	{
		return request;
	}
	function getResponseString()
	{
		if (request.responseText != null)
			return request.responseText;
		else
			return "";
	}
	function getResponseXml()
	{
		return request.responseXML;
	}
	function setEventMonior(currEventId, currAllowedEventIdsCsv)
	{
		currentEventId = currEventId;
		allowedEventIdsCsv = currAllowedEventIdsCsv;
	}
	function showStatus(elementId, text, backgroundColor)
	{
		window.document.getElementById(elementId).innerHTML = text;
		window.document.getElementById(elementId).style.backgroundColor = backgroundColor;
	}
	function showStatusImg(elementId, imageUrl)
	{
		window.document.getElementById(elementId).src = imageUrl;
	}
	function isEventConsumed()
	{
		return (request.readyState == 4 || request.readyState == "complete");
	}
	function beginEvent(eventId)
	{
		this.setEventMonior(eventId, null);
	}
	function endEvent()
	{
		this.setEventMonior(null, null);
	}
	
	// Internal Methods

	function callUrl(postOrGet, url, eventId, callbackConsumer)
	{
		try
		{
			param = null;
			if (currentEventId == null || isAllowedEventId(eventId))
			{
				request = getXmlHttpRequest();
				if (request != null)
				{
					currentEventId = eventId;
					request.onreadystatechange = callbackConsumer;
					param = this.parseEventParam(eventId);
					var ajaxUrl = initAjaxCallUrl(url, eventId, param);
					var asynchronous = true;
					request.open(postOrGet, ajaxUrl, asynchronous);
					if (postOrGet == "POST")
						request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					request.send(null);
				}
				else alert("Can not create XMLHttpRequest object...");
			}
			else alert("Please wait, " + currentEventId + " is currently runing...");
		}
		catch (ex)
		{
			alert("callUrl exception: " + ex.description);
		}
	}
	function getXmlHttpRequest()
	{
		var xmlHttpReq;
		try
		{
			if (window.XMLHttpRequest != null) // For IE7 and Mozilla browsers
				xmlHttpReq = new XMLHttpRequest();
			else if (window.ActiveXObject) // for IE 5.0 and IE 6.0 browsers
			{
				try
				{
					xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (msEx)
				{
					xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
				}
			}
		}
		catch (ex)
		{
			alert("getXmlHttpRequest exception: " + ex.description);
			xmlHttpReq = null;
		}
		return xmlHttpReq;
	}
	function initAjaxCallUrl(url, eventId, params)
	{
		var urlEventId = "e";
		var urlParam = "p";
		var urlDate = "d";

		var ajaxUrlParams =
			urlEventId + "=" + eventId
			+ (params != null ? "&" + urlParam + "=" + params : "")
			+ "&" + urlDate + "=" + new Date().toUTCString();

		if (url.indexOf("?") == -1)
			return url + "?" + ajaxUrlParams;
		else
			return url + "&" + ajaxUrlParams;
	}
	function isAllowedEventId(eventId)
	{
		if (allowedEventIdsCsv == null || eventId.length > allowedEventIdsCsv.length)
			return false;
		else if (eventId == allowedEventIdsCsv)
			return true;
		else if (allowedEventIdsCsv.indexOf("," + eventId + ",") > 0)
			return true;
		else if (allowedEventIdsCsv.indexOf(eventId + ",") == 0)
			return true;
		else if (allowedEventIdsCsv.indexOf("," + eventId) > 0)
			return true;
		else
			return false;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

radumi
Software Developer
New Zealand New Zealand
Coder

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 22 Jan 2013
Article Copyright 2008 by radumi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid