A tiny and minimal module for HTTP requests. Returns a promise.
$ npm install nathan7/xhr
or
$ component install nathan7/xhr
Perform a GET request to the given URL with the given options.
The options
object has the following properties:
The url to perform the request on.
The HTTP method to use (GET, POST, PUT, DELETE, etc.). Defaults to GET
.
An object containing headers.
headers: {
'Accept': 'application/json'
}
The data to send along as the body of the request.
Browser-only.
If true, the withCredentials
value will be applied to the XMLHttpRequest object, which allows for CORS requests.
var xhr = require('xhr')
var fooData = JSON.stringify({
foo: 'bar',
baz: 'qux'
})
xhr({
url: 'http://example.com/foos',
headers: {
'Content-Type': 'application/json',
'Content-Length': fooData.length
},
method: 'POST',
data: fooData
}.then(function onSuccess() {
console.log('It worked!')
}
, function onError(err) {
console.log('There was an error: ' + err.message)
})