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

Add content type when possible #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "corsRequest",
"version": "0.0.1",
"version": "0.0.2",
"authors": [
"Tiago Quelhas <tiagoq@gmail.com>"
],
Expand Down
31 changes: 22 additions & 9 deletions corsRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
}
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -153,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
Expand All @@ -163,6 +163,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);
}


Expand Down Expand Up @@ -203,6 +212,10 @@
}


// Default timeout value.
corsRequest.timeout = 10000;


// Report implementation in use.
corsRequest.usingXhr = useXhr;
corsRequest.usingXdr = useXdr;
Expand Down