1.2.1 • Published 8 years ago
xhr-promise-redux v1.2.1
Originally forked from frikille/promised-xhr
XHR Promise
This module wraps XMLHttpRequest with in promise object. The promise implementation is Promises/A+ compliant and is provided by the promise.js Promise library.
Installation
Install via npm
$ npm install xhr-promise-redux
API
var xhr = require('xhr-promise-redux');
xhr.get(url, options)
xhr.post(url, options)
xhr.send(url, options)
Examples
Sending a GET request
xhr.get('/test-url', { data: { param: 'value' }, headers: { 'Header-name': 'Header value' }, responseType: 'json' }) .then(function (response) { console.log(`Success! The response JSON object: ${response.body}`); }) .catch(function(response) { console.log(`Error! Response Status Code: ${response.statusCode}`) });
Sending a POST request with JSON
xhr.post('/test-url', { json: { param: 'value' }, headers: { 'Header-name': 'Header value' }, responseType: 'json' }) .then(function (response) { console.log(`Success! The response JSON object: ${response.body}`); }) .catch(function(response) { console.log(`Error! Response Status Code: ${response.statusCode}`) });
Sending a request with any method
xhr.send('/test-url', { method: 'PUT', json: { param: 'value' }, headers: { 'Header-name': 'Header value' } }) .then(function (response) { console.log(`Success! The response JSON object: ${response.body}`); }) .catch(function(response) { console.log(`Error! Response Status Code: ${response.statusCode}`) });