Posting time: 10:15:00
VaR HTTP = {};
// This is a list of XMLHttpRequest-creation factory functions to try
HTTP. _ factories = [
Function () {return New XMLHttpRequest ();},
Function () {return New activexobject ("msxml2.xmlhttp ");},
Function () {return New activexobject ("Microsoft. XMLHTTP ");}
];
// When we find a factory that works, store it here.
HTTP. _ factory = NULL;
// Create and return a new XMLHTTPRequest object.
//
// The first time we're called, try the list of factory functions
// We find one that returns a non-null value and does not throw
// Exception. Once we find a working factory, remember it for later use.
//
HTTP. newrequest = function (){
If (HTTP. _ factory! = NULL) return HTTP. _ Factory ();
For (VAR I = 0; I Try {
VaR factory = http. _ factories [I];
VaR request = Factory ();
If (request! = NULL ){
HTTP. _ factory = factory;
Return request;
}
}
Catch (e ){
Continue;
}
}
// If we get here, none of the factory candidates succeeded,
// So throw an exception now and for all future CILS.
HTTP. _ factory = function (){
Throw new error ("XMLHttpRequest not supported ");
}
HTTP. _ Factory (); // throw an error
}
/**
* Use XMLHttpRequest to fetch the contents of the specified URL using
* An http get request. When the response arrives, pass it (as plain
* Text) to the specified callback function.
*
* This function does not block and has no return value.
* Http. gettext ('default2. aspx ', showtext );
*
* Function showtext (STR ){
* Alert (STR );
*}
*/
HTTP. gettext = function (URL, callback ){
VaR request = http. newrequest ();
Request. onreadystatechange = function (){
If (request. readystate = 4 & request. Status = 200)
Callback (request. responsetext );
}
Request. Open ("get", URL );
Request. Send (null );
};
HTTP. getxml = function (URL, callback ){
VaR request = http. newrequest ();
Request. onreadystatechange = function (){
If (request. readystate = 4 & request. Status = 200)
Callback (request. responsexml );
}
Request. Open ("get", URL );
Request. Send (null );
};
/**
* Use an HTTP head request to obtain the headers for the specified URL.
* When the headers arrive, parse them with HTTP. parseheaders () and pass
* Resulting object to the specified callback function. If the server returns
* An error code, invoke the specified errorhandler function instead. If no
* Error handler is specified, pass null to the callback function.
* Example:
* Http. getheaders ('default2. aspx ', showheaders, showerror );
*
* Function showerror (status, statustext ){
* Alert (status + ':' + statustext );
*}
*
* Function showheaders (OBJ ){
* If (OBJ! = NULL ){
* For (var I in OBJ ){
* Alert (OBJ [I]);
*}
*}
*}
*/
HTTP. getheaders = function (URL, callback, errorhandler ){
VaR request = http. newrequest ();
Request. onreadystatechange = function (){
If (request. readystate = 4 ){
If (request. Status = 200 ){
Callback (HTTP. parseheaders (request ));
}
Else {
If (errorhandler) errorhandler (request. status,
Request. statustext );
Else callback (null );
}
}
}
Request. Open ("head", URL );
Request. Send (null );
};
// Parse the response headers from an XMLHTTPRequest object and return
// The header names and values as property names and values of a new object.
HTTP. parseheaders = function (request ){
VaR headertext = request. getAllResponseHeaders (); // text from the server
VaR headers ={}; // This will be our return value
VaR ls =/^ "s */; // leading space Regular Expression
VaR Ts =/"s * $/; // trailing space Regular Expression
// Break the headers into lines
VaR lines = headertext. Split ("" N ");
// Loop through the lines
For (VAR I = 0; I <lines. length; I ++ ){
VaR line = lines [I];
If (line. Length = 0) continue; // skip empty lines
// Split each line at first colon, and trim whitespace away
VaR Pos = line. indexof (':');
VaR name = line. substring (0, POS). Replace (LS, ""). Replace (TS ,"");
VaR value = line. substring (Pos + 1). Replace (LS, ""). Replace (TS ,"");
// Store the header name/value pair in a JavaScript Object
Headers [name] = value;
}
Return headers;
};
/**
* Send an http post request to the specified URL, using the names and values
* Of the properties of the values object as the body of the request.
* Parse the server's response according to its content type and pass
* The resulting value to the callback function. If an HTTP Error occurs,
* Call the specified errorhandler function, or pass null to the callback
* If no error handler is specified.
**/
HTTP. Post = function (URL, values, callback, errorhandler ){
VaR request = http. newrequest ();
Request. onreadystatechange = function (){
If (request. readystate = 4 ){
If (request. Status = 200 ){
Callback (HTTP. _ getresponse (request ));
}
Else {
If (errorhandler) errorhandler (request. status,
Request. statustext );
Else callback (null );
}
}
}
Request. Open ("Post", URL );
// This header tells the server how to interpret the body of the request.
Request. setRequestHeader ("Content-Type ",
"Application/X-WWW-form-urlencoded ");
// Encode the properties of the values object and send them
// The Body of the request.
Request. Send (HTTP. encodeformdata (values ));
};
/**
* Encode the property name/value pairs of an object as if they were from
* An HTML form, using application/X-WWW-form-urlencoded format
*/
HTTP. encodeformdata = function (data ){
VaR pairs = [];
VaR Regexp =/% 20/g; // a regular expression to match an encoded Space
For (VAR name in data ){
VaR value = data [name]. tostring ();
// Create a name/value pair, but encode name and value first
// The global function encodeuricomponent does almost what we want,
// But It encodes spaces as % 20 instead of as "+". We have
// Fix that with string. Replace ()
VaR pair = encodeuricomponent (name). Replace (Regexp, "+") + '=' +
Encodeuricomponent (value). Replace (Regexp, "+ ");
Pairs. Push (pair );
}
// Concatenate all the name/value pairs, separating them &
Return pairs. Join ('&');
};
HTTP. _ getresponse = function (request ){
// Check the content type returned by the server
Switch (request. getResponseHeader ("Content-Type ")){
Case "text/XML ":
// If it is an XML document, use the parsed Document Object.
Return request. responsexml;
Case "text/JSON ":
Case "text/JavaScript ":
Case "application/JavaScript ":
Case "application/X-JavaScript ":
// If the response is JavaScript code, or a JSON-encoded value,
// Call eval () on the text to "parse" it to a javascript value.
// Note: only do this if the JavaScript code is from a trusted server!
Return eval (request. responsetext );
Default:
// Otherwise, treat the response as plain text and return as a string.
Return request. responsetext;
}
};
/**
* Send an http get request for the specified URL. If a successful
* Response is wrongly Ed, it is converted to an object based on
* Content-Type header and passed to the specified callback function.
* Additional arguments may be specified as properties of the options object.
*
* If an error response is received (e.g., A 404 Not Found error ),
* The status code and message are passed to the options. errorhandler
* Function. If no error handler is specified, the callback
* Function is called instead with a null argument.
*
* If the options. Parameters object is specified, its properties are
* Taken as the names and values of Request Parameters. They are
* Converted to a URL-encoded string with HTTP. encodeformdata () and
* Are appended to the URL following '? '.
*
* If an options. progresshandler function is specified, it is
* Called each time the readystate property is set to some value less
* Than 4. Each call to the progress-handler function is passed
* Integer that specifies how many times it has been called.
*
* If an options. Timeout value is specified, the XMLHttpRequest
* Is aborted if it has not completed before the specified number
* Of milliseconds have elapsed. If the timeout elapses and
* Options. timeouthandler is specified, that function is called
* The requested URL as its argument.
**/
HTTP. Get = function (URL, callback, options ){
VaR request = http. newrequest ();
VaR n = 0;
VaR timer;
If (options. Timeout)
Timer = setTimeout (function (){
Request. Abort ();
If (options. timeouthandler)
Options. timeouthandler (URL );
},
Options. Timeout );
Request. onreadystatechange = function (){
If (request. readystate = 4 ){
If (timer) cleartimeout (timer );
If (request. Status = 200 ){
Callback (HTTP. _ getresponse (request ));
}
Else {
If (options. errorhandler)
Options. errorhandler (request. status,
Request. statustext );
Else callback (null );
}
}
Else if (options. progresshandler ){
Options. progresshandler (++ N );
}
}
VaR target = URL;
If (options. Parameters)
Target + = "? "+ HTTP. encodeformdata (options. Parameters)
Request. Open ("get", target );
Request. Send (null );
};