Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > AjaxRequest.js XMLHttpRequest Helper Class

Reply
Thread Tools

AjaxRequest.js XMLHttpRequest Helper Class

 
 
ignatz
Guest
Posts: n/a
 
      04-03-2005
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.cg...est.js#dirlist
TEST PAGE:
http://www.ad1455.com/lib/js/ajaxlib...axRequest.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 <(E-Mail Removed)>
* @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[i]+'.XMLHTTP')
}
catch (e) { }
}
throw new Error("No XML component installed!")
}
}

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Helper class uses main class' pointer Nephi Immortal C++ 1 02-09-2011 11:56 AM
Convenience class vs helper class Crocodile Java 0 09-22-2008 12:50 PM
What's the purpose of the helper cache and is there a way to just turn off helper cache-ing? kevin Ruby 1 07-21-2006 03:42 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Problem with singleton helper class for java rmi server. Arijit Mukherjee Java 0 01-02-2004 05:01 AM



Advertisments