From e39e2d812d3926ad1fce2d1e52de66d80718102d Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 29 Apr 2014 13:28:11 -0700 Subject: [PATCH 1/2] get XMLHttpRequest at call time so that we can replace if we want to, such as when mocking --- index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1a77c6f..74a064b 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -var XHR = XMLHttpRequest -if (!XHR) throw new Error('missing XMLHttpRequest') +var GetXHR = function(){ + if(!XMLHttpRequest){ + throw new Error('missing XMLHttpRequest'); + } + return XMLHttpRequest; +} + request.log = { 'trace': noop, 'debug': noop, 'info': noop, 'warn': noop, 'error': noop } @@ -164,7 +169,8 @@ function request(options, callback) { var req_seq = 0 function run_xhr(options) { - var xhr = new XHR + var XHR = GetXHR(); + var xhr = new XHR() , timed_out = false , is_cors = is_crossDomain(options.uri) , supports_cors = ('withCredentials' in xhr) From 64f40de0627d9c31b40b022e2175f598bbb6b21e Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 14 May 2014 12:51:29 -0700 Subject: [PATCH 2/2] =?UTF-8?q?Don=E2=80=99t=20error=20on=20JSON=20parse?= =?UTF-8?q?=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It should be up to the caller to decide if a non-JSON body is actually an error state. For example: you make a request where you expect a JSON response, but instead you get 204 No Content. It should be up to your application to decide what that means. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 1a77c6f..ea33518 100644 --- a/index.js +++ b/index.js @@ -271,7 +271,7 @@ function run_xhr(options) { xhr.body = xhr.responseText if(options.json) { try { xhr.body = JSON.parse(xhr.responseText) } - catch (er) { return options.callback(er, xhr) } + catch (e) { } } options.callback(null, xhr, xhr.body)