This documents is for open source project 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>
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);
});
Bind events with API on to do something when event fired. The event include: ready、success、fail、error.
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);
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);
});