xhr-promise-redux

1.2.1 • Public • Published

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 version

$ 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

  1. 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}`)
  });
  1. 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}`)
  });
  1. 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}`)
  });

Readme

Keywords

Package Sidebar

Install

npm i xhr-promise-redux

Weekly Downloads

306

Version

1.2.1

License

MIT

Last publish

Collaborators

  • cbargren
  • dancinghacker
  • implausibility
  • maxkorp
  • mr-wallet