Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IE XDomainRequest support #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ http.request = function (params, cb) {
if (!params.host) params.host = window.location.host.split(':')[0];
if (!params.port) params.port = window.location.port;

var req = new Request(new xhrHttp, params);
var req = new Request(new xhrHttp(params), params);
if (cb) req.on('response', cb);
return req;
};
Expand All @@ -22,10 +22,13 @@ http.get = function (params, cb) {
http.Agent = function () {};
http.Agent.defaultMaxSockets = 4;

var xhrHttp = (function () {
var xhrHttp = function (params) {
if (typeof window === 'undefined') {
throw new Error('no window object present');
}
else if (params.host != window.location.host.split(':')[0] && window.XDomainRequest) {
return window.XDomainRequest;
}
else if (window.XMLHttpRequest) {
return window.XMLHttpRequest;
}
Expand Down Expand Up @@ -56,4 +59,4 @@ var xhrHttp = (function () {
else {
throw new Error('ajax not supported in this browser');
}
})();
};
17 changes: 17 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,26 @@ var Request = module.exports = function (xhr, params) {
self.emit('response', res);
});


xhr.onprogress = function() { // IE XDR
xhr.readyState = 2;
res.getAllResponseHeaders = function() {
return 'Content-Type: ' + xhr.contentType; // This is the only header available
};
res.handle(xhr);
}
xhr.onreadystatechange = function () {
res.handle(xhr);
};
xhr.onload = function() { // IE XDR
xhr.readyState = 4;
res.handle(xhr);
};
xhr.onerror = function() { // IE XDR
xhr.readyState = 3;
xhr.error = 'Unknown error';
res.handle(xhr);
};
};

Request.prototype = new EventEmitter;
Expand Down