XHR wrapper, with Node-style callbacks.
var Request = ;// instantiatevar request = 'post' 'http://example.com/'; request data firstname: 'Anna' lastname: 'Sedokova' ; { if err throw err; // }
Installation
Install with component:
$ component install decanat/request
Install with npm:
$ npm install decanat-request
API
Headers
req;req; reqheaders; // { 'x-csrf-token': 't0ken', 'x-hello': 'moto', ... }
Data
var req = ; req data'She' 'looks' data'like=no%20one&in=history' data 'She': 'stings' 'like': 'every girl' ; reqbody; // '{"She":"stings","like":"every girl","in":"history"}'
Body will be attached to querystring on GET
and HEAD
requests. So if previous request was initialized with 'get'
as method, we'd have:
reqbody; // nullrequrl; // 'http://example.com/bangs?She=stings&like=every%20girl&in=history'
Callbacks
Callback function can be supplied as argument, when calling .send
method:
req;
Or using .complete
, which saves callback to be invoked when XHR request will be loaded, or immediately, if it's alread loaded.
// `fn1`, `fn2`, .. are callback functionsreq completefn1 completefn2; req;
Testing
To test with PhantomJS, run:
$ make serve &
$ make test
Forebears
License
The MIT License (MIT)