README
smallxhr
Very Small XHR library for easy, minimal cross-browser requests.
Usage
Basic GET
var xhr = require('smallxhr');
// GET example.com
xhr('http://example.com', null, function(error, data, xhrResponse) {
if (error) {
throw error;
}
console.log('status: ' + xhrResponse.status);
console.log(data);
});
Complete POST
var xhr = require('smallxhr');
var postData = JSON.stringify({foo: 'bar'});
// POST example.com with data
xhr('http://example.com', postData, function(error, data, xhrResponse) {
if (error) {
throw error;
}
console.log('status: ' + xhrResponse.status);
console.log(data);
}, 'POST', 'application/json', 30000);
API
smallxhr(url, data, callback, method, contenttype, timeout, headers)
url
- The url to send the request todata
- Data to send with the request. Should be a string ornull
.callback
- Executed on success or failure with params (error, data, xhrResponse)method
- Any HTTP verbcontenttype
- Any HTTP content type (default:application/x-www-form-urlencoded
)timeout
- time in milliseconds before the request should be cancelled (default:5000
)headers
- key-value pairs for specifying extra headers to be sent (default:{}
)
Callback
function(error, data, xhrResponse)
error
- AnError
object with a custom attributetype
error.type
- one of'timeout'
for a timeout error, or'http'
to indicate you should check the value ofxhrResponse.statusCode
.
data
- Any data returned from the requestxhrResponse
- The XMLHttpRequest object
Installation
npm install --save smallxhr
Thanks
- Simon Doodkin's original tinyxhr gist
- Scott Duncombe's updates