AjaxRequest.js XMLHttpRequest Helper Class

I

ignatz

I'm interested in receiving comments on the following XMLHttpRequest
helper class that I've written. It is designed to simplify handling
synchronous and asyncronous gets. I know that it has make my life a lot
easier but I'm wondering if there ways that it can be improved to get
it closer to some sort of utopian ideal.

USAGE:
var http = new AjaxRequest()
http.setTarget("/lib/ajaxlib/test/ping.php")
http.setForm(new URLBuilder("word", "ping"))
var synchronousGet = http.get()
var synchronousPost = http.post()
// Asynchronous
http.get(closureHere)
http.post(closureHere)

FILE: http://www.ad1455.com/lib/js/ajaxlib/AjaxRequest.js
VIEWCVS:
http://cvs.ad1455.com/cgi/viewcvs.cgi/ajaxlib/AjaxRequest.js#dirlist
TEST PAGE:
http://www.ad1455.com/lib/js/ajaxlib/test/testAjaxRequest.html

A tar that includes the PHP ping page that I used to test the library
is @ http://www.ad1455.com/lib/tars/js.ajaxlib.tar.gz

=>Chris
http://www.advogato.org/person/ignatz/

p.s. Here's the Class:

/**
* @file AjaxRequest.js
*
* AD1455.com XMLHttpRequest Client Library
*
* @author Copyright 2005 C. Baker <[email protected]>
* @author Licensed under the Academic Free License 2.1
http://www.opensource.org/licenses/afl-2.1.php
*/

////////////////////////////////////////////////////////////////////////////////
// AjaxRequest

function AjaxRequest(returnType)
{
// Are we returning XML or Text
if (returnType) {
this._returnType = returnType
}
else
{
this._returnType = AjaxRequest.RESPONSE_TEXT
}

this.xmlhttp = new XMLHttpRequest()

this.requestHeaders = new Object();
this.responseHeaders = new Object();

// Target defaults to self
this._target = document.URL

this._form
}

// Static constants
AjaxRequest.libName = "AjaxRequest"
AjaxRequest.libVersion = 1.0

/**
* Constant for returning XML
*/
AjaxRequest.RESPONSE_XML = "responseXML"

/**
* Constant for returning Text
*/
AjaxRequest.RESPONSE_TEXT = "responseText"


AjaxRequest.prototype.getTarget = function()
{
return this._target
}

AjaxRequest.prototype.setTarget = function(target)
{
this._target = target
}

AjaxRequest.prototype.getForm = function()
{
return this._form
}

AjaxRequest.prototype.setForm = function(form)
{
if (typeof(form) == "object")
{
this._form = form.toString()
}
else
{
this._form = form
}
}

AjaxRequest.prototype._getURI = function()
{
if (this._form) {
return this._target + "?" + this._form
}
return this._target
}

AjaxRequest.prototype.get = function(closure)
{
if (closure)
{
this.asynchronousGet(closure)
}
return this.synchronousGet()
}

AjaxRequest.prototype.post = function(closure)
{
if (closure)
{
this.asynchronousPost(closure)
}
return this.synchronousPost()
}

AjaxRequest.prototype.asynchronousGet = function(closure)
{
// This, as in the this Object, no longer refers to this in closures,

// so we need to copy it.
// A good article on this is
// http://w3future.com/html/stories/callbacks.xml
var me = this

// For asynchronous calls you can't use on
var http = new XMLHttpRequest()
http.open("GET", this._getURI(), true)

http.onreadystatechange = function()
{
if(http.readyState == 4)
{
http.onreadystatechange = function(){}

if (closure != null)
{
closure(http[me._returnType])
}
}
};
http.send(null)
}

AjaxRequest.prototype.asynchronousPost = function(closure)
{
var me = this
var http = new XMLHttpRequest()

http.open("POST", this.getTarget(), true);

http.setRequestHeader("Content-Type","application/x-www-form-urlencoded;
charset=UTF-8")

http.onreadystatechange = function()
{
if(http.readyState == 4)
{
http.onreadystatechange = function(){}
closure(http[me._returnType])
}
};
http.send(this.getForm())
}


AjaxRequest.prototype.synchronousGet = function()
{
var ret = ""

this.xmlhttp.open("GET", this._getURI(), false)

for(header in this.requestHeaders)
{
this.xmlhttp.setRequestHeader(header, this.requestHeaders[header])
}

this.xmlhttp.send(null)
if (this.xmlhttp.status == 200)
{
ret = this.xmlhttp.responseText
}
return ret
}

AjaxRequest.prototype.synchronousPost = function(target, content)
{
var ret = ""

this.xmlhttp.open("POST", this.getTarget(), false)

// Needed for form posting

this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;
charset=UTF-8")
for(header in this.requestHeaders)
{
this.xmlhttp.setRequestHeader(header, this.requestHeaders[header])
}

this.xmlhttp.send(this.getForm())
if (this.xmlhttp.status == 200)
{
ret = this.xmlhttp.responseText
}
return ret
}

// AjaxRequest
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// URLBuilder

/**
* Class to facilitate GET and POST query strings.
*
* Vars can be passed in through the constructor:
*
* var u = new URLBuilder("word", "ping", "name", "pang")
*
* u.toString() returns "word=ping&name=pang"
*/
function URLBuilder()
{
this._vars = new Object()

if (arguments.length > 0)
{
for (var x = 0; x < arguments.length; x = x + 2)
{
if (arguments[x + 1] == undefined)
{
arguments[x + 1] = ""
}
this.addVar(arguments[x], arguments[x + 1])
}
}
}

URLBuilder.prototype.addVar = function(key, val)
{
this._vars[encodeURIComponent(key)] = encodeURIComponent(val)
}

URLBuilder.prototype.toString = function()
{
var delim = ''
var url = ''
for (key in this._vars)
{
url += delim + key + '=' + this._vars[key]
delim = '&'
}
return url
}

// URLBuilder
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// XMLHttpRequest

/**
* Cross-browser XMLHttpRequest instantiation. Posted by Gyoung-Yoon
Noh
* on the Ruby on Rails mailing list.
*
* Usage:
*
* xmlhttp = new XMLHttpRequest()
*
* @author Gyoung-Yoon Noh
* @link http://hieraki.goodlad.ca/read/chapter/8#page16
*/
if (typeof XMLHttpRequest == 'undefined')
{
XMLHttpRequest = function ()
{
var msxmls = ['MSXML3', 'MSXML2', 'Microsoft']
for (var i=0; i < msxmls.length; i++)
{
try
{
return new ActiveXObject(msxmls+'.XMLHTTP')
}
catch (e) { }
}
throw new Error("No XML component installed!")
}
}

// XMLHttpRequest
////////////////////////////////////////////////////////////////////////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top