The xhr module implements support for making HTTP requests using XMLHttpRequest.
$ component install anchorjs/xhr
$ volo add anchorjs/xhr
xhr.request()
returns an instance of Request
. If one needs to upload data
with a POST request:
var req = xhr.request('/upload', 'POST', function(res) {
res.on('end', function() {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
console.log('BODY: ' + res.responseText);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.send('data\n');
Since most requests are GET requests without bodies, Anchor provides get()
as
a convenience method. The only difference between this method and xhr.request()
is that it sets the method to GET and calls req.send()
automatically.
xhr.get("/user.json", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
This module uses the AMD format. To include in component builds, use component-amd:
component build -u component-amd
To run tests in a browser, execute the Make target for the desired browser:
$ make test-chrome
$ make test-firefox
$ make test-safari
Headless tests can be executed directly from a terminal:
$ make test-phantomjs
Copyright (c) 2012-2013 Jared Hanson <http://jaredhanson.net/>