Originally forked from frikille/promised-xhr
This module wraps XMLHttpRequest with in promise object. The promise implementation is Promises/A+ compliant and is provided by the promise.js Promise library.
Install via npm
$ npm install xhr-promise-redux
var xhr = require('xhr-promise-redux');
xhr.get(url, options)
xhr.post(url, options)
xhr.send(url, options)
- 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}`)
});