Skip to content

Commit

Permalink
add withCredentials attribute support to XMLHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
eychu committed Nov 19, 2014
1 parent 768a321 commit b0e915f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ The default timeout is `Qajax.defaults.timeout` and can be overriden.
var p = Qajax({ url: "/", headers: { "X-Foo": "bar" } });
```

* You can set withCredentials attribute for [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing), see also: https://xhr.spec.whatwg.org/#the-withcredentials-attribute.

```javascript
var p = Qajax({ url: "http://another-domain.com/", withCredentials: true });
```

* You can give a `cancellation` Promise to abort an ajax request.

Here is a typical use case:
Expand Down
4 changes: 2 additions & 2 deletions build/qajax.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions docs/qajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,14 @@ <h1>qajax.js</h1>
*/</span>
<span class="hljs-comment">/*jslint newcap: true */</span>
(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(definition)</span> </span>{
<span class="hljs-keyword">var</span> Q;
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> exports === <span class="hljs-string">"object"</span>) {
Q = <span class="hljs-built_in">require</span>(<span class="hljs-string">"q"</span>);
<span class="hljs-built_in">module</span>.exports = definition(Q);
<span class="hljs-built_in">module</span>.exports = definition(<span class="hljs-built_in">require</span>(<span class="hljs-string">"q"</span>));
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> define === <span class="hljs-string">'function'</span> &amp;&amp; define.amd){
define([<span class="hljs-string">'q'</span>], definition);
}
<span class="hljs-keyword">else</span> {
Q = <span class="hljs-built_in">window</span>.Q;
<span class="hljs-built_in">window</span>.Qajax = definition(Q);
Qajax = definition(Q);
}
})(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(Q)</span> </span>{
<span class="hljs-pi"> "use strict"</span>;</pre></div></div>
Expand Down Expand Up @@ -112,6 +109,7 @@ <h2 id="parameters">Parameters</h2>
<li><code>data</code> <strong>(any)</strong> <em>optional</em>: the data to send.</li>
<li>headers <strong>(object)</strong> <em>optional</em>: a map of headers to use for the XHR.</li>
<li><code>responseType</code> <strong>(string)</strong> <em>optional</em>: a responseType to set the XHR with.</li>
<li><code>withCredentials</code> <strong>(boolean)</strong> <em>optional</em>: set the XHR withCredentials attribute <em>(default: false)</em>.</li>
<li><code>cancellation</code> <strong>(Promise)</strong> <em>optional</em>: provide a “cancellation” promise which if fulfilled will cancel the current XHR.</li>
<li><strong>Or any other parameter from the Qajax.defaults</strong>.</li>
</ul>
Expand Down Expand Up @@ -166,7 +164,8 @@ <h2 id="result">Result</h2>
xhrResult = Q.defer(),
timeout = getOrElse(<span class="hljs-string">"timeout"</span>, settings),
headers = extend1({}, getOrElse(<span class="hljs-string">"headers"</span>, settings)),
cacheParam = getOrElse(<span class="hljs-string">"cache"</span>, settings);
cacheParam = getOrElse(<span class="hljs-string">"cache"</span>, settings),
withCredentials = getOrElse(<span class="hljs-string">"withCredentials"</span>, settings);

<span class="hljs-keyword">if</span> (cacheParam) {
params[cacheParam === <span class="hljs-literal">true</span> ? <span class="hljs-string">"_"</span> : cacheParam] = (<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()).getTime();
Expand Down Expand Up @@ -302,6 +301,10 @@ <h2 id="result">Result</h2>
<span class="hljs-keyword">if</span> (headers.hasOwnProperty(h)) {
xhr.setRequestHeader(h, headers[h]);
}
}

<span class="hljs-keyword">if</span> (withCredentials) {
xhr.withCredentials = <span class="hljs-literal">true</span>;
}</pre></div></div>

</li>
Expand Down Expand Up @@ -495,7 +498,7 @@ <h1 id="qajax-defaults">Qajax Defaults</h1>

</div>

<div class="content"><div class='highlight'><pre> cache: <span class="hljs-built_in">window</span>.ActiveXObject || <span class="hljs-string">"ActiveXObject"</span> <span class="hljs-keyword">in</span> <span class="hljs-built_in">window</span>,</pre></div></div>
<div class="content"><div class='highlight'><pre> cache: !!(<span class="hljs-built_in">window</span>.ActiveXObject || <span class="hljs-string">"ActiveXObject"</span> <span class="hljs-keyword">in</span> <span class="hljs-built_in">window</span>),</pre></div></div>

</li>

Expand All @@ -506,7 +509,7 @@ <h1 id="qajax-defaults">Qajax Defaults</h1>
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">&#182;</a>
</div>
<p><code>method</code> <strong>(string)</strong>: The default HTTP method to apply when calling Qajax(url) </p>
<p><code>method</code> <strong>(string)</strong>: The default HTTP method to apply when calling Qajax(url)</p>

</div>

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"name": "Paul Dijou",
"email": "paul.dijou+dev@gmail.com",
"url": "http://pauldijou.com"
},
{
"name": "Alexey Chuvashov",
"email": "alexeychu@gmail.com"
}
],
"dependencies": {
Expand Down
8 changes: 7 additions & 1 deletion src/qajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
// - `data` **(any)** *optional*: the data to send.
// - headers **(object)** *optional*: a map of headers to use for the XHR.
// - `responseType` **(string)** *optional*: a responseType to set the XHR with.
// - `withCredentials` **(boolean)** *optional*: set the XHR withCredentials attribute *(default: false)*.
// - `cancellation` **(Promise)** *optional*: provide a "cancellation" promise which if fulfilled will cancel the current XHR.
// - **Or any other parameter from the Qajax.defaults**.
//
Expand Down Expand Up @@ -77,7 +78,8 @@
xhrResult = Q.defer(),
timeout = getOrElse("timeout", settings),
headers = extend1({}, getOrElse("headers", settings)),
cacheParam = getOrElse("cache", settings);
cacheParam = getOrElse("cache", settings),
withCredentials = getOrElse("withCredentials", settings);

if (cacheParam) {
params[cacheParam === true ? "_" : cacheParam] = (new Date()).getTime();
Expand Down Expand Up @@ -139,6 +141,10 @@
}
}

if (withCredentials) {
xhr.withCredentials = true;
}

// Send the XHR
if (data !== undefined && data !== null) {
xhr.send(data);
Expand Down
12 changes: 12 additions & 0 deletions test/qajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ asyncTest("simple Qajax() successful", 1, function() {
.fin(start);
});

asyncTest("simple Qajax() and withCredentials successful", 1, function() {
resetDefaults();
function checkResult (res) {
deepEqual(res, sample01json, "sample01.json successfully retrieved.");
}
Qajax(sample01url, { withCredentials: true })
.then(Qajax.filterSuccess)
.then(Qajax.toJSON)
.then(checkResult, checkNotError)
.fin(start);
});

asyncTest("failure when 404 Not Found", 2, function() {
resetDefaults();
function checkError (e) {
Expand Down

0 comments on commit b0e915f

Please sign in to comment.