Package Exports
- xhr
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xhr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
xhr
A small xhr wrapper. Designed for use with browserify.
Browser support: IE8+ and everything else.
Example
var xhr = require("xhr")
xhr({
body: someJSONString,
uri: "/foo",
headers: {
"Content-Type": "application/json"
}
}, function (err, resp, body) {
// check resp.statusCode
})var req = xhr(options, callback)
type XhrOptions = String | {
useXDR: Boolean?,
sync: Boolean?,
uri: String,
url: String,
method: String?,
timeout: Number?,
headers: Object?,
body: String?,
json: Object?,
username: String?,
password: String?,
withCredentials: Boolean?,
responseType: String?
}
xhr := (XhrOptions, Callback<Response>) => Requestthe returned object is either an XMLHttpRequest instance
or an XDomainRequest instance (if on IE8/IE9 &&
options.useXDR is set to true)
Your callback will be called once with the arguments
( Error, response , body ) where the response is an object:
{
body: Object||String,
statusCode: Number,
method: String,
headers: {},
url: String,
rawRequest: xhr
} body: HTTP response body -xhr.response,xhr.responseTextorxhr.responseXMLdepending on the request type.rawRequest: OriginalXMLHttpRequestinstance orXDomainRequestinstance (if on IE8/IE9 &&options.useXDRis set totrue)headers: A collection of headers where keys are header names converted to lowercase
Your callback will be called with an Error if there is an error in the browser that prevents sending the request.
A HTTP 500 response is not going to cause an error to be returned.
If options is a string then it's a short hand for
{ method: "GET", uri: string }
options.method
Specify the method the XMLHttpRequest should be opened
with. Passed to xhr.open. Defaults to "GET"
options.useXDR
Specify whether this is a cross origin (CORS) request for IE<10.
Switches IE to use XDomainRequest instead of XMLHttpRequest.
Ignored in other browsers.
Note that headers cannot be set on an XDomainRequest instance.
options.sync
Specify whether this is a synchrounous request. Note that when this is true the callback will be called synchronously. In most cases this option should not be used. Only use if you know what you are doing!
options.body
Pass in body to be send across the XMLHttpRequest.
Generally should be a string. But anything that's valid as
a parameter to xhr.send should work
options.uri or options.url
The uri to send a request to. Passed to xhr.open. options.url and options.uri are aliases for each other.
options.headers
An object of headers that should be set on the request. The
key, value pair is passed to xhr.setRequestHeader
options.timeout
Number of miliseconds to wait for response. Defaults to 0 (no timeout). Ignored when options.sync is true.
options.json
A valid JSON serializable value to be send to the server. If this
is set then we serialize the value and use that as the body.
We also set the Content-Type to "application/json".
Additionally the response body is parsed as JSON
options.withCredentials
Specify whether user credentials are to be included in a cross-origin
request. Sets xhr.withCredentials. Defaults to false.
A wildcard * cannot be used in the Access-Control-Allow-Origin header when withCredentials is true.
The header needs to specify your origin explicitly or browser will abort the request.
options.responseType
Determines the data type of the response. Sets xhr.responseType. For example, a responseType of document will return a parsed Document object as the response.body for an XML resource.
options.xhr
Pass an XMLHttpRequest object (or something that acts like one) to use instead of constructing a new one using the XMLHttpRequest or XDomainRequest constructors. Useful for testing.