From 70bd1a1639c111523bf02dc5d161566c60e7e9d3 Mon Sep 17 00:00:00 2001 From: Derek Kastner Date: Thu, 22 Mar 2012 14:25:50 -0400 Subject: [PATCH] IE XDomainRequest support for IE CORS --- index.js | 9 ++++++--- lib/request.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index bca5a27..5d24b5e 100644 --- a/index.js +++ b/index.js @@ -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; }; @@ -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; } @@ -56,4 +59,4 @@ var xhrHttp = (function () { else { throw new Error('ajax not supported in this browser'); } -})(); +}; diff --git a/lib/request.js b/lib/request.js index 19ecca3..f5ad1b7 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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;