All Projects → hustcc → Xhr.js

hustcc / Xhr.js

Licence: mit
🌎 xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Xhr.js

Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (+2133.33%)
Mutual labels:  ajax, fetch, xhr
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (+275%)
Mutual labels:  fetch, xhr, ajax
vue-methods-promise
Let Vue methods support return Promise
Stars: ✭ 35 (+191.67%)
Mutual labels:  fetch, ajax
axios-endpoints
Axios endpoints helps you to create a more concise endpoint mapping with axios.
Stars: ✭ 41 (+241.67%)
Mutual labels:  fetch, ajax
Redux Requests
Declarative AJAX requests and automatic network state management for single-page applications
Stars: ✭ 330 (+2650%)
Mutual labels:  ajax, fetch
htmx-talk-2021
Code examples and slides from my 2021 talk Server-Side is Dead! Long Live Server-Side (+ HTMX), presented at DjangoCon and Code Code Code
Stars: ✭ 18 (+50%)
Mutual labels:  ajax, requests
sapper-httpclient
An isomorphic http client for Sapper
Stars: ✭ 48 (+300%)
Mutual labels:  fetch, xhr
i18next-http-backend
i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
Stars: ✭ 270 (+2150%)
Mutual labels:  fetch, xhr
Kkjsbridge
一站式解决 WKWebView 支持离线包,Ajax/Fetch 请求,表单请求和 Cookie 同步的问题 (基于 Ajax Hook,Fetch Hook 和 Cookie Hook)
Stars: ✭ 462 (+3750%)
Mutual labels:  ajax, fetch
Atomic
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
Stars: ✭ 526 (+4283.33%)
Mutual labels:  ajax, xhr
Unfetch
🐕 Bare minimum 500b fetch polyfill.
Stars: ✭ 5,239 (+43558.33%)
Mutual labels:  ajax, fetch
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (+266.67%)
Mutual labels:  fetch, requests
angular-progress-http
[DEPRECATED] Use @angular/common/http instead
Stars: ✭ 43 (+258.33%)
Mutual labels:  xhr, ajax
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+1066.67%)
Mutual labels:  fetch, ajax
http
Tiny, embeddable HTTP client with simple API for the browser
Stars: ✭ 21 (+75%)
Mutual labels:  xhr, ajax
Ironwing
universal, framework agnostic, transport layer
Stars: ✭ 17 (+41.67%)
Mutual labels:  ajax, xhr
upload-file-to-backblaze-b2-from-browser-example
Demonstrates calling the b2_upload_file Backblaze B2 Cloud Storage API from a web browser using AJAX.
Stars: ✭ 28 (+133.33%)
Mutual labels:  xhr, ajax
react-sync
A declarative approach to fetching data via a React higher order component
Stars: ✭ 18 (+50%)
Mutual labels:  fetch, ajax
Resource Loader
A middleware-style generic resource loader built with web games in mind.
Stars: ✭ 364 (+2933.33%)
Mutual labels:  ajax, xhr
Restclient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮
Stars: ✭ 675 (+5525%)
Mutual labels:  requests, ajax

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

npm install xhr.js

2. import xhr.js

UMD import is supported, then get global object: XHR.

import XHR from 'xhr.js';

// or

var XHR = require("xhr.js");

or link with script in html files:

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

3. use class XHR

var xhr = XHR(async); // default is async. you can set sync use  XHR(false)
xhr.on('success', function(result) {
	console.log('status:', result.status);
	console.log('statusText:', result.statusText);
	console.log('url:', result.url);
	console.log('responseType:', result.responseType);
	console.log('text:', result.text);
	console.log('headers:', result.headers);

	console.log('ok:', result.ok()); // get the json result.
	console.log('json:', result.json()); // get the json result.
	console.log('xml:', result.xml());
	console.log('blob:', result.blob());
});

xhr.get('package.json', {'a': 'b'});

Another post demo:

var xhr = XHR();
xhr.post('/post_url', {'a': 'b'}, function(r) {
	r = r.json(); // get the json result.
	// write your code
});

Upload file with FormData object:

var fd = new FormData(document.querySelector('#submit_form'));

var xhr = new XHR();
xhr.post('/submit/new', fd, function(r) {
    // request success
    r = r.json();
    console.log(r);
});

Detail API

1. XHR API

The API of xhr instance.

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

2. XHR Event key

The evnet keys is for API on.

  1. ready: when xhr is ready.
  2. success: when status_code is 200.
  3. fail: when status_code is not 200.

3. Response API

The api is for request callback function paramter result.

  1. result.text: get all response text;
  2. result.status: the server response code;
  3. result.statusText: the server response code text, e.g. ok (status code is 200).
  4. result.responseType: response type;
  5. result.headers: get all the response headers object;
  6. result.ok(): is request ok;
  7. result.json(): get the json object of response text;
  8. result.xml(): get the xml object of response text;
  9. result.blob(): get the blob object of response text;

TODO

  • request auth
  • a http test chrome plugin, like postman.

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].