Skip to content

Commit

Permalink
Add a examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Aug 10, 2013
1 parent 9646cef commit f8a3786
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/handleevent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(function(global) {
'use strict';

var XMLHttpRequest = require('../lib/xmlhttprequest');

var obj = {
name: 'XMLHttpRequest...',
handleEvent: function(event) {
var client = event.target;
var response = client.response;
console.log('==== %s ====', this.name);
console.log('%s %s', client.status, client.statusText);
console.log(client.getAllResponseHeaders());
console.log(response.split('\n').slice(0, 3).join('\n'));
console.log('...');
}
};

function request(uri) {
var client;
if (!uri) {
throw new TypeError('URI is required for the argument.');
}
client = new XMLHttpRequest();
client.open('GET', uri);
client.responseType = 'text';
client.addEventListener('load', obj, false);
client.send(null);
}

function main(argv) {
try {
request.apply(this, argv.slice(2));
} catch (error) {
console.error(error.message);
}
}

main(process.argv);
})(this);
25 changes: 25 additions & 0 deletions examples/responsetype-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(function(global) {
'use strict';

var XMLHttpRequest = require('../lib/xmlhttprequest');

var API_URI = 'http://registry.npmjs.org/w3c-xmlhttprequest';

function request(searchKeyword) {
var client = new XMLHttpRequest();
client.open('GET', API_URI);
client.responseType = 'json';
client.addEventListener('load', function(event) {
var response = client.response;
var result = JSON.stringify(response, null, ' ');
console.log(result);
});
client.send(null);
}

function main(argv) {
request.apply(this, argv.slice(2));
}

main(process.argv);
})(this);

0 comments on commit f8a3786

Please sign in to comment.