xhr.js

This documents is for open source project xhr.js.

1. Install xhr.js

npm install xhr.js

Then import xhr.js

import xhr from "xhr.js"

or use script tag:

<script src="dist/xhr.min.js"></script>

2. Get / Post request

Send a HTTP get or post request.

Synopsis: new XHR().get(url, params, onsuccess, onfail)

var xhr = XHR();
xhr.get('package.json', {'a': 'b'}, function(r) {
  r = r.json(); // the json result.
  alert(r.name + '\n' + r.summary);
});

3. on Event

Bind events with API on to do something when event fired. The event include: readysuccessfailerror.

Synopsis: new XHR().on(event, callback)

var xhr = XHR();
xhr.on('success', function(r) {
  r = r.json(); // the json result.
  alert(r.name + ' code is here: ' + r.repository.url);
});
xhr.request('GET', 'package.json', null);

4. Sync request

Send a sync request.

Synopsis: new XHR(async)

var xhr = XHR(false);
xhr.request('DELETE', 'package.json', null, function(r) {
  r = r.json();
  alert(r.name + ' code is here: ' + r.repository.url);
}, function(r) {
  alert('Request error: \n' + r.status_code + '\n' + r.status_text);
});

The source of xhr.js here. Detail documents here.