A small client-side XHR abstraction with promises.
XHR.get(url, options)
XHR.post(url, options)
XHR.send(url, options)
- Sending a GET request
var XHR = require('promised-xhr');
XHR.get('/test-url', {
data: {
param: 'value'
},
headers: {
'Header-name': 'Header value'
}
})
.then(function (response) {
console.log('Success handler');
console.log('The response JSON obejct', response.body);
})
.catch(function(response) {
console.log('Error handler')
});
- Sending a POST request
var XHR = require('promised-xhr');
XHR.post('/test-url', {
data: {
param: 'value'
},
headers: {
'Header-name': 'Header value'
}
})
.then(function (response) {
console.log('Success handler');
console.log('The response JSON obejct', response.body);
})
.catch(function(response) {
console.log('Error handler')
});
- Sending a request with any method
var XHR = require('promised-xhr');
XHR.send('/test-url', {
method: 'PUT',
data: {
param: 'value'
},
headers: {
'Header-name': 'Header value'
}
})
.then(function (response) {
console.log('Success handler');
console.log('The response JSON obejct', response.body);
})
.catch(function(response) {
console.log('Error handler')
});