From f7c26f8aa8ee89024c3a47d261ad1de24a03a438 Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Thu, 26 Jun 2014 13:07:49 +0100 Subject: [PATCH 1/4] Add default timeout --- corsRequest.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/corsRequest.js b/corsRequest.js index fc7c0aa..f2bf19a 100644 --- a/corsRequest.js +++ b/corsRequest.js @@ -107,11 +107,6 @@ this.req.open(this.method, this.url); - this.req.ontimeout = function() { - self.err = error('timeout'); - done(self.err); - }; - this.req.onerror = function() { self.err = error('error'); done(self.err); @@ -138,9 +133,10 @@ done(self.err, self.res); }; - // Setting this handler seems to increase the probability + // Setting all handlers seems to increase the probability // of the request being sucessful on old IE versions. if (useXdr) { + this.req.ontimeout = noop; this.req.onprogress = noop; } @@ -163,6 +159,15 @@ self.req.send(payload || null); } }, 0); + + // Perform our own timeout logic instead of relying on + // the underlying implementation. + setTimeout(function() { + if (self.err === void 0) { + self.err = error('timeout'); + done(self.err); + } + }, corsRequest.timeout); } @@ -203,6 +208,10 @@ } + // Default timeout value. + corsRequest.timeout = 10000; + + // Report implementation in use. corsRequest.usingXhr = useXhr; corsRequest.usingXdr = useXdr; From 603da6b96ac1b1dafa66d54366152f9de8ee694a Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Thu, 26 Jun 2014 13:08:22 +0100 Subject: [PATCH 2/4] Version bump --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 823301e..7274633 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "corsRequest", - "version": "0.0.1", + "version": "0.0.2", "authors": [ "Tiago Quelhas " ], From 827637a24e82b16950fa2236bd82d35e057d1fb2 Mon Sep 17 00:00:00 2001 From: Manuel Cabral Date: Fri, 19 Sep 2014 14:31:20 +0100 Subject: [PATCH 3/4] Set Content-Type when possible --- corsRequest.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/corsRequest.js b/corsRequest.js index f2bf19a..2e9ff44 100644 --- a/corsRequest.js +++ b/corsRequest.js @@ -81,9 +81,9 @@ // Encode data to JSON representation or string, according to its type. function encode(data) { if (isObject(data)) { - return toJSON(data); + return { data: toJSON(data), contentType: 'application/json' }; } else { - return data.toString(); + return { data: data.toString(), contentType: 'text/plain' }; } } @@ -149,7 +149,11 @@ }; if (this.data !== void 0) { - var payload = encode(data); + encoded = encode(data) + var payload = encoded.data; + if(this.req.setRequestHeader) { + this.req.setRequestHeader('Content-Type', encoded.contentType); + } } // Calling send() right away sometimes causes the request From be3c6296eeb518909524b590c5285b9768f26faf Mon Sep 17 00:00:00 2001 From: Manuel Cabral Date: Fri, 19 Sep 2014 14:37:46 +0100 Subject: [PATCH 4/4] Mention express-ie-cors on docs --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b40bb7..feb607d 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,9 @@ If supplied, the *done* callback must have signature (err, res). The request is * *res.text*: the raw response body. * *res.json*: the response body parsed as JSON, if it is valid JSON. -All functions return an object with properties *method*, *url*, *data* and *req* (the underlying request object). Properties *err* and *res* are set when the request completes. Call *abort()* on the object to abort the request. \ No newline at end of file +All functions return an object with properties *method*, *url*, *data* and *req* (the underlying request object). Properties *err* and *res* are set when the request completes. Call *abort()* on the object to abort the request. + + +## Content-Type ## + +Using XDomainRequest it is not possible to set the request's `Content-Type` header. If using express, [express-ie-cors](https://github.com/advanced/express-ie-cors) provides a workaround. \ No newline at end of file