xhr.js

xhr.js is a library(<2Kb) to make AJAX/HTTP restful requests from client-side JavaScript with XMLHttpRequest.

  • xhr.js

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
xhr.js
1701.0.28 years ago8 years agoMinified + gzip package size for xhr.js in KB

Readme

xhr.js
xhr.js is a library(< 2Kb) to make AJAX/HTTP restful requests with XMLHttpRequest. It has similar API with Python-requests.
Build Status npm npm npm
Usage
1. Install xhr.js ```sh npm install xhr.js ``` 2. import xhr.js UMD import is supported, then get global object: XHR. ```js import XHR from 'xhr.js'; // or var XHR = require("xhr.js"); ``` or link with script in html files: ```js ``` 3. use class XHR ```js var xhr = XHR(async); // default is async. you can set sync use XHR(false) xhr.on('success', function(result) {
console.log('status_code:', result.status_code);
console.log('status_text:', result.status_text);
console.log('response_type:', result.response_type);
console.log('text:', result.text);
console.log('headers:', result.headers());
console.log('json:', result.json()); // get the json result.
console.log('xml:', result.xml());
}); xhr.get('package.json', params={'a': 'b'}); ```
Detail API

  1. XHR API

  1. xhr.request(method, url, body, onsuccess, onfail): request the url, with the method.
  1. xhr.on(): bind the request result(ready, error, success, fail), with result instance as it input.
  1. xhr.get(url, params, onsuccess, onfail): get request.
  1. xhr.post(url, params, onsuccess, onfail): post request.
  1. xhr.setRequestHeader(header_name, header_value): append a header.
  1. xhr.setAsync(aysnc): set request async / sync.
  1. xhr.url(): get the request url.
  1. xhr.body(): get the request body.
  1. xhr.abort(): abort request.
  1. xhr.reset(): reset the xhr instance, such url, headers, body, events.

  1. XHR Event

  1. ready: when xhr is ready.
  1. error: when xhr can not be ready.
  1. success: when status_code is 200.
  1. fail: when status_code is not 200.

  1. Response API (result)

  1. result.text: get all response text;
  1. result.status_code: the server response code;
  1. result.status_text: the server response code text, e.g. ok (status code is 200).
  1. result.response_type: response type;
  1. result.json: get the json object of response text;
  1. result.xml: get the xml object of response text;
  1. result.headers: get all the response headers object;
TODO
- request auth - delete, put - a http test chrome plugin, like postman.
LICENSE
MIT