AjaxRequest.js XMLHttpRequest Helper Class

Discussion in 'Javascript' started by ignatz, Apr 3, 2005.

  1. ignatz

    ignatz Guest

    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 <>
    * @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
    ////////////////////////////////////////////////////////////////////////////////
    ignatz, Apr 3, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arijit Mukherjee
    Replies:
    0
    Views:
    672
    Arijit Mukherjee
    Jan 2, 2004
  2. Crocodile

    Convenience class vs helper class

    Crocodile, Sep 22, 2008, in forum: Java
    Replies:
    0
    Views:
    444
    Crocodile
    Sep 22, 2008
  3. Nephi Immortal

    Helper class uses main class' pointer

    Nephi Immortal, Feb 8, 2011, in forum: C++
    Replies:
    1
    Views:
    452
    Saeed Amrollahi
    Feb 9, 2011
  4. kevin
    Replies:
    1
    Views:
    369
    Kairi Zikpin
    Jul 21, 2006
  5. KnighT
    Replies:
    4
    Views:
    87
    KnighT
    Sep 14, 2006
Loading...

Share This Page