Net - A simple, robust XMLHTTP
interface
Net is an XMLHTTP connection management library meant to simplify JavaScript authors' dealings with the underlying code necessary to successfully make both GET and POST requests.
Obtaining
The latest version of the code will always be available from this page.
Net.js - (4.8KB) - 2006-08-29
Usage
Public Methods
get
static- Parameters
oArgs- a hash (or object) that expects certain key/value pairs:url- string containing the absolute or relative URL to which the request will be made.- [optional]
vars- a string or object literal containing keys and values to pass along with the request. onsuccess- a function reference to a function taking a single argument, the request object. This function will be called upon connection success.- [optional]
onerror- a function reference to a function to handle errors. See_fnErrorDefaultfor format information.
- Return Value -
trueon success,falseon failure. - Summary - dispatches a GET request to the given URL and passes the
given parameters. Calls
onsuccesson success with the request object as the only argument, oronerroron failure.
post
static- Parameters
oArgs- a hash (or object) that expects certain key/value pairs:url- string containing the absolute or relative URL to which the request will be made.- [optional]
vars- a string or object literal containing keys and values to pass along with the request. onsuccess- a function reference to a function taking a single argument, the request object. This function will be called upon connection success.- [optional]
onerror- a function reference to a function to handle errors. See_fnErrorDefaultfor format information.
- Return Value -
trueon success,falseon failure. - Summary - dispatches a POST request to the given URL and passes the
given parameters. Calls
onsuccesson success with the request object as the only argument, oronerroron failure.
Example Usage 1
var fnWhenDone = function(R) { alert(R.responseText); };
Net.get({url: "foo.xml", vars: "x=1&y=2", onsuccess: fnWhenDone});
Explanation
Probably the simplest invocation of Net.get() with a
simple relative URL, a string of '&' delimited
key=value pairs, and a callback function that simply alerts
the responseText property of the R
object.
Example Usage 2
var fnWhenDone = function(R) { alert(R.responseText); };
var fnWhenError = function(sType, R) { alert("Error! ("+sType+"): "+(R.statusText||'')); };
Net.post({url: "foo.php", vars: {x: 1, y: 2}, onsuccess: fnWhenDone, onerror: fnWhenError});
Explanation
A call to Net.post() that passes an object literal with
key-value pairs for the vars and both a success and error handler.
Test Script
See test.html for a working implementation.
Private Methods
Net [constructor]
- Summary: called internally to construct a new instance of the Net class for each request.
_createRequestObject
static- Parameters - none.
- Return Value - a reference to a valid request object or
null. - Summary - creates a request object and returns a reference to it.
_fnErrorDefault
static- Parameters
sType- a string indicating the type of error.Request- the request object involved with the error.
- Return Value - none.
- Summary - receives an error type, one of: "timeout",
"initialization", or some other message such as an HTTP failure code.
This method is an empty shell and will be used by default unless a
function reference for an error handling function is passed into the
original
Net.get()orNet.post()call.
_serializeObject
static- Parameters
oFrom- an object containing key-value pairs.
- Return Value - a string with the contents of the object passed in, URI-encoded and '&'-delimited.
- Summary - serializes a JS object into a URI-encoded string of key-value pairs.
_setCallback
- Parameters
fnCallback- a function reference to the function that is to be called on success of the request.fnError- a function reference to the function that is to be called in the case of an error.
- Return Value - none.
- Summary - binds the success and error callback functions to the request instance.
Meta
- License: Creative Commons Attribution-ShareAlike License
- Original Author: Brad Fults <bfults@gmail.com>
- Last Updated: 2007-02-19