3 A small XMLHttpRequest wrapper. Designed for use with [browserify](http://browserify.org/), [webpack](https://webpack.github.io/) etc.
5 API is a subset of [request](https://github.com/request/request) so that the same code can be used in the browser and Node.js.
7 Browser support: IE8+ and everything else.
12 var xhr = require("xhr")
18 "Content-Type": "application/json"
20 }, function (err, resp, body) {
21 // check resp.statusCode
25 ## `var req = xhr(options, callback)`
28 type XhrOptions = String | {
40 withCredentials: Boolean?,
41 responseType: String?,
44 xhr := (XhrOptions, Callback<Response>) => Request
46 the returned object is either an [`XMLHttpRequest`][3] instance
47 or an [`XDomainRequest`][4] instance (if on IE8/IE9 &&
48 `options.useXDR` is set to `true`)
50 Your callback will be called once with the arguments
51 ( [`Error`][5], `response` , `body` ) where the response is an object:
62 - `body`: HTTP response body - [`XMLHttpRequest.response`][6], [`XMLHttpRequest.responseText`][7] or
63 [`XMLHttpRequest.responseXML`][8] depending on the request type.
64 - `rawRequest`: Original [`XMLHttpRequest`][3] instance
65 or [`XDomainRequest`][4] instance (if on IE8/IE9 &&
66 `options.useXDR` is set to `true`)
67 - `headers`: A collection of headers where keys are header names converted to lowercase
70 Your callback will be called with an [`Error`][5] if there is an error in the browser that prevents sending the request.
71 A HTTP 500 response is not going to cause an error to be returned.
75 * `var req = xhr(url, callback)` -
76 a simple string instead of the options. In this case, a GET request will be made to that url.
78 * `var req = xhr(url, options, callback)` -
79 the above may also be called with the standard set of options.
82 * `var req = xhr.{post, put, patch, del, head, get}(url, callback)`
83 * `var req = xhr.{post, put, patch, del, head, get}(options, callback)`
84 * `var req = xhr.{post, put, patch, del, head, get}(url, options, callback)`
86 The `xhr` module has convience functions attached that will make requests with the given method.
87 Each function is named after its method, with the exception of `DELETE` which is called `xhr.del` for compatibility.
89 The method shorthands may be combined with the url-first form of `xhr` for succinct and descriptive requests. For example,
92 xhr.post('/post-to-me', function(err, resp) {
93 console.log(resp.body)
100 xhr.del('/delete-me', { headers: { my: 'auth' } }, function (err, resp) {
101 console.log(resp.statusCode);
109 Specify the method the [`XMLHttpRequest`][3] should be opened
110 with. Passed to [`XMLHttpRequest.open`][2]. Defaults to "GET"
114 Specify whether this is a cross origin (CORS) request for IE<10.
115 Switches IE to use [`XDomainRequest`][4] instead of `XMLHttpRequest`.
116 Ignored in other browsers.
118 Note that headers cannot be set on an XDomainRequest instance.
122 Specify whether this is a synchrounous request. Note that when
123 this is true the callback will be called synchronously. In
124 most cases this option should not be used. Only use if you
125 know what you are doing!
129 Pass in body to be send across the [`XMLHttpRequest`][3].
130 Generally should be a string. But anything that's valid as
131 a parameter to [`XMLHttpRequest.send`][1] should work (Buffer for file, etc.).
133 ### `options.uri` or `options.url`
135 The uri to send a request to. Passed to [`XMLHttpRequest.open`][2]. `options.url` and `options.uri` are aliases for each other.
137 ### `options.headers`
139 An object of headers that should be set on the request. The
140 key, value pair is passed to [`XMLHttpRequest.setRequestHeader`][9]
142 ### `options.timeout`
144 Number of miliseconds to wait for response. Defaults to 0 (no timeout). Ignored when `options.sync` is true.
148 A valid JSON serializable value to be send to the server. If this
149 is set then we serialize the value and use that as the body.
150 We also set the Content-Type to `"application/json"`.
152 Additionally the response body is parsed as JSON
154 ### `options.withCredentials`
156 Specify whether user credentials are to be included in a cross-origin
157 request. Sets [`XMLHttpRequest.withCredentials`][10]. Defaults to false.
159 A wildcard `*` cannot be used in the `Access-Control-Allow-Origin` header when `withCredentials` is true.
160 The header needs to specify your origin explicitly or browser will abort the request.
162 ### `options.responseType`
164 Determines the data type of the `response`. Sets [`XMLHttpRequest.responseType`][11]. For example, a `responseType` of `document` will return a parsed `Document` object as the `response.body` for an XML resource.
166 ### `options.beforeSend`
168 A function being called right before the `send` method of the `XMLHttpRequest` or `XDomainRequest` instance is called. The `XMLHttpRequest` or `XDomainRequest` instance is passed as an argument.
172 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.
176 - Why is my server's JSON response not parsed? I returned the right content-type.
177 - See `options.json` - you can set it to `true` on a GET request to tell `xhr` to parse the response body.
178 - Without `options.json` body is returned as-is (a string or when `responseType` is set and the browser supports it - a result of parsing JSON or XML)
179 - How do I send an object or array as POST body?
180 - `options.body` should be a string. You need to serialize your object before passing to `xhr` for sending.
181 - To serialize to JSON you can use
182 `options.json` instead of `options.body` for convenience - then `xhr` will do the serialization and set content-type accordingly.
183 - Where's stream API? `.pipe()` etc.
184 - Not implemented. You can't reasonably have that in the browser.
188 You can override the constructor used to create new requests for testing. When you're making a new request:
191 xhr({ xhr: new MockXMLHttpRequest() })
194 or you can override the constructors used to create requests at the module level:
197 xhr.XMLHttpRequest = MockXMLHttpRequest
198 xhr.XDomainRequest = MockXDomainRequest
203 [1]: http://xhr.spec.whatwg.org/#the-send()-method
204 [2]: http://xhr.spec.whatwg.org/#the-open()-method
205 [3]: http://xhr.spec.whatwg.org/#interface-xmlhttprequest
206 [4]: http://msdn.microsoft.com/en-us/library/ie/cc288060(v=vs.85).aspx
207 [5]: http://es5.github.com/#x15.11
208 [6]: http://xhr.spec.whatwg.org/#the-response-attribute
209 [7]: http://xhr.spec.whatwg.org/#the-responsetext-attribute
210 [8]: http://xhr.spec.whatwg.org/#the-responsexml-attribute
211 [9]: http://xhr.spec.whatwg.org/#the-setrequestheader()-method
212 [10]: http://xhr.spec.whatwg.org/#the-withcredentials-attribute
213 [11]: https://xhr.spec.whatwg.org/#the-responsetype-attribute