Skip to content

Latest commit

 

History

History
77 lines (50 loc) · 1.78 KB

README.md

File metadata and controls

77 lines (50 loc) · 1.78 KB

Anchor/XHR

The xhr module implements support for making HTTP requests using XMLHttpRequest.

Install

component
$ component install anchorjs/xhr
volo
$ volo add anchorjs/xhr

Usage

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);
});

Compatibility

component

This module uses the AMD format. To include in component builds, use component-amd:

component build -u component-amd

Tests

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

Credits

License

The MIT License

Copyright (c) 2012-2013 Jared Hanson <http://jaredhanson.net/>