XML HTTP Request WrapperSimple Javascript Wrapper for the XMLHttpRequest Object. Supports IE5.5+, Firefox 1.0+, Opera, Safari.
For IE6- it will use the IE's XMLHttpRequest ActiveX Object and cache the version for faster Instantiation (though it doesn't really matter compared to HTTP latency).
The HTTP Requests use HTTP/1.1 where available which will persist the TCP connection and reduce the overhead of constructing and destructing the TCP connection for each HTTP Request.
By default the requests url encoded GET and POST requests. (ie: it automatically urlencodes the POST data). The Content-Type needs to be overridden if you need to send RAW HTTP POST requests such as XML or Binary Data etc.
ExamplesIn these examples, the URL is set to http://example.com. This URL would have to be changed to your domain. You can also use relative URLs.
Full Examples can be found in SVN.
Send a HTTP Get Request and show the HTTP Response Textnew fiji.xhr('get', 'http://example.com/?param=value', function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
}).send();The callback function is scoped to the XMLHttpRequest Instance so you can use this in the callback function to reference the XHR instance.
Send a HTTP POST Request with some URL encoded Datanew fiji.xhr('post', 'http://example.com/', function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
}).send({'param': 'value'});HTTP Polling// fiji.xhr instance
var XHR = new fiji.xhr();
// callback function
callback = function() {
if (this.readyState == 4) {
if (this.status == 200) {
// poll server every 1/2 seconds
setTimeout(function() {
XHR.req('post', 'echo.php', callback);
XHR.send({'param': 'value'});
}, 500);
} else {
alert('HTTP Error: '+this.status);
}
}
};
// set the request parameters
XHR.req('post', 'http://example.com/', callback);
// send initial request
XHR.send({'param': 'value'});Send a Different Content-Type// fiji.xhr instance
var XHR = new fiji.xhr();
// set the request
XHR.req('post', 'http://example.com/', function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
});
// override the request content-type header with an XML content-type
XHR.xhr.setRequestHeader("Content-Type", "text/xml");
// send HTTP POST Request with XML body
XHR.send('');DownloadsCheckout the latest version from SVN.
30 Day Summary Apr 12 2013 — May 12 2013
|
12 Month Summary May 12 2012 — May 12 2013
|
Copyright
©
2013
Black Duck Software, Inc.
and its contributors, Some Rights Reserved. Unless otherwise marked, this work is licensed under a
Creative Commons Attribution 3.0 Unported License
. Ohloh
®
and the Ohloh logo are trademarks of
Black Duck Software, Inc.
in the United States and/or other jurisdictions. All other trademarks are the property of their respective holders.