Skip to content

Commit 9535087

Browse files
Add documentation for ajaxSetup() and xhrFields
1 parent 6b4dec5 commit 9535087

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

can-ajax.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,31 @@ var param = require("can-param");
3636
* - __data__ `{Object}` The data of the request. If data needs to be urlencoded (e.g. for GET requests or for CORS) it is serialized with [can-param].
3737
* - __dataType__ `{String}` Type of data. _Default is `json`_.
3838
* - __crossDomain__ `{Boolean}` If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. Default: `false` for same-domain requests, `true` for cross-domain requests.
39-
*
39+
* - __xhrFields__ `{Object}` Any fields to be set directly on the xhr request, [https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest] such as the withCredentials attribute that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.
40+
*
4041
* @return {Promise} A Promise that resolves to the data. The Promise instance is abortable and exposes an `abort` method. Invoking abort on the Promise instance indirectly rejects it.
4142
*
43+
*
44+
* @signature `ajaxSetup( ajaxOptions )`
45+
*
46+
* Is used to persist ajaxOptions across all ajax requests and they can be over-written in the ajaxOptions of the actual request.
47+
* [https://api.jquery.com/jquery.ajaxsetup/]
48+
*
49+
* ```
50+
* var ajax = require("can-ajax");
51+
*
52+
* ajax.ajaxSetup({xhrFields: {withCredentials: true}});
53+
*
54+
* ajax({
55+
* url: "http://query.yahooapis.com/v1/public/yql",
56+
* data: {
57+
* format: "json",
58+
* q: 'select * from geo.places where text="sunnyvale, ca"'
59+
* }
60+
* }).then(function(response){
61+
* console.log( response.query.count ); // => 2
62+
* });
63+
* ```
4264
*/
4365

4466
// from https://gist.github.com/mythz/1334560
@@ -202,11 +224,11 @@ function ajax(o) {
202224
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
203225
}
204226

205-
if (o.xhrFields) {
206-
for (var f in o.xhrFields) {
207-
xhr[f] = o.xhrFields[f];
227+
if (o.xhrFields) {
228+
for (var f in o.xhrFields) {
229+
xhr[f] = o.xhrFields[f];
230+
}
208231
}
209-
}
210232

211233
xhr.send(data);
212234
return promise;

0 commit comments

Comments
 (0)