Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
matthewp/xhr, as promised.
JavaScript
branch: master

2.2.1

latest commit c977b47768
@nathan7 authored
Failed to load latest commit information.
.gitignore
.jshintrc refactor
README.md 1.0.0
component.json v2.2.0
core-browser.js
core.js
index.js
package.json 2.2.1

README.md

xhr

A tiny and minimal module for HTTP requests. Returns a promise.

Installation

$ npm install nathan7/xhr

or

$ component install nathan7/xhr

API

xhr(url, options)

Perform a GET request to the given URL with the given options.

options

The options object has the following properties:

url

The url to perform the request on.

method

The HTTP method to use (GET, POST, PUT, DELETE, etc.). Defaults to GET.

headers

An object containing headers.

headers: {
  'Accept': 'application/json'
}
data

The data to send along as the body of the request.

credentials

Browser-only. If true, the withCredentials value will be applied to the XMLHttpRequest object, which allows for CORS requests.

Examples

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)
  })
Something went wrong with that request. Please try again.