smallxhr

1.4.0 • Public • Published

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 to
  • data - Data to send with the request. Should be a string or null.
  • callback - Executed on success or failure with params (error, data, xhrResponse)
  • method - Any HTTP verb
  • contenttype - 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 - An Error object with a custom attribute type
    • error.type - one of 'timeout' for a timeout error, or 'http' to indicate you should check the value of xhrResponse.statusCode.
  • data - Any data returned from the request
  • xhrResponse - The XMLHttpRequest object

Installation

npm install --save smallxhr

Thanks

Dependents (10)

Package Sidebar

Install

npm i smallxhr

Weekly Downloads

106

Version

1.4.0

License

ISC

Last publish

Collaborators

  • jesstelford