Skip to content

cbargren/xhr-promise-redux

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%