Skip to content

Commit cdcccbf

Browse files
authored
Merge pull request #38 from canjs/major
can-ajax version 2
2 parents dde5e12 + 6f0f53b commit cdcccbf

File tree

6 files changed

+83
-58
lines changed

6 files changed

+83
-58
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ node_modules
2929

3030
docs/
3131
dist/
32+
/.idea/
33+
/package-lock.json

.npmignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
!dist/
1+
!dist/
2+
/test/
3+
/.idea/
4+
/can-ajax-test.js
5+
/can-ajax-test-result.json
6+
/can-ajax-test-result.txt
7+
/test.html

can-ajax-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,42 @@ QUnit.test("It doesn't stringify FormData", function(assert) {
514514
done();
515515
});
516516
});
517+
518+
QUnit.test("beforsend", function (assert) {
519+
var done = assert.async();
520+
var headers = {},
521+
restore = makeFixture(function () {
522+
this.open = function (type, url) {};
523+
524+
this.send = function () {
525+
this.readyState = 4;
526+
this.status = 204;
527+
this.responseText = '';
528+
this.onreadystatechange();
529+
};
530+
531+
this.setRequestHeader = function (header, value) {
532+
headers[header] = value;
533+
};
534+
});
535+
536+
ajax({
537+
type: "post",
538+
url: "/foo",
539+
data: {
540+
id: "qux"
541+
},
542+
dataType: "json",
543+
beforeSend: function (xhr){
544+
xhr.setRequestHeader("Authorization", "Bearer 123");
545+
}
546+
}).then(function (value) {
547+
assert.ok(headers.hasOwnProperty('Authorization'), "custom header set");
548+
}, function (reason) {
549+
assert.notOk(reason, "request failed with reason = ", reason);
550+
}).then(function () {
551+
// restore original values
552+
restore();
553+
done();
554+
});
555+
});

can-ajax.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var param = require("can-param");
3838
* - __dataType__ `{String}` Type of data. _Default is `json`_.
3939
* - __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.
4040
* - __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.
41+
* - __beforeSend__ `{callback}` A pre-request callback function that can be used to modify the XHR object before it is sent. Use this to set custom headers, etc. The XHR and settings objects are passed as arguments.
4142
*
4243
* @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.
4344
*
@@ -101,19 +102,26 @@ var contentTypes = {
101102
};
102103

103104
var _xhrResp = function (xhr, options) {
104-
switch (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]) {
105-
case "text/xml":
106-
case "xml":
107-
return xhr.responseXML;
108-
case "text/json":
109-
case "application/json":
110-
case "text/javascript":
111-
case "application/javascript":
112-
case "application/x-javascript":
113-
case "json":
114-
return xhr.responseText && JSON.parse(xhr.responseText);
115-
default:
116-
return xhr.responseText;
105+
var type = (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]);
106+
107+
if(type && (xhr.responseText || xhr.responseXML)){
108+
109+
switch (type) {
110+
case "text/xml":
111+
case "xml":
112+
return xhr.responseXML;
113+
case "text/json":
114+
case "application/json":
115+
case "text/javascript":
116+
case "application/javascript":
117+
case "application/x-javascript":
118+
case "json":
119+
return xhr.responseText && JSON.parse(xhr.responseText);
120+
default:
121+
return xhr.responseText;
122+
}
123+
} else {
124+
return xhr;
117125
}
118126
};
119127

@@ -186,7 +194,7 @@ function ajax(o) {
186194
if (xhr.status >= 200 && xhr.status < 300) {
187195
deferred.resolve( _xhrResp(xhr, o) );
188196
} else {
189-
deferred.reject( xhr );
197+
deferred.reject( _xhrResp(xhr, o) );
190198
}
191199
}
192200
else if (o.progress) {
@@ -232,11 +240,15 @@ function ajax(o) {
232240
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
233241
}
234242

235-
if (o.xhrFields) {
236-
for (var f in o.xhrFields) {
237-
xhr[f] = o.xhrFields[f];
238-
}
239-
}
243+
if(o.beforeSend){
244+
o.beforeSend.call( o, xhr, o );
245+
}
246+
247+
if (o.xhrFields) {
248+
for (var f in o.xhrFields) {
249+
xhr[f] = o.xhrFields[f];
250+
}
251+
}
240252

241253
xhr.send(data);
242254
return promise;

index.html

Lines changed: 0 additions & 25 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"release:minor": "npm version minor && npm publish",
2525
"release:major": "npm version major && npm publish",
2626
"build": "node build.js",
27-
"develop": "done-serve --static --develop --port 8080",
2827
"detect-cycle": "detect-cyclic-packages --ignore done-serve"
2928
},
3029
"main": "dist/cjs/can-ajax",
@@ -37,13 +36,8 @@
3736
],
3837
"steal": {
3938
"main": "can-ajax",
40-
"configDependencies": [
41-
"live-reload"
42-
],
4339
"npmIgnore": [
4440
"testee",
45-
"generator-donejs",
46-
"donejs-cli",
4741
"steal-tools"
4842
]
4943
},
@@ -52,18 +46,15 @@
5246
"can-namespace": "^1.0.0",
5347
"can-param": "^1.0.1",
5448
"can-parse-uri": "^1.0.0",
55-
"can-reflect": "^1.4.5"
49+
"can-reflect": "^1.16.3"
5650
},
5751
"devDependencies": {
5852
"can-make-map": "^1.0.0",
5953
"detect-cyclic-packages": "^1.1.0",
60-
"done-serve": "^1.0.0",
61-
"donejs-cli": "^1.0.0",
62-
"generator-donejs": "^1.0.0",
6354
"jshint": "^2.9.1",
64-
"steal": "^1.5.10",
55+
"steal": "^1.11.8",
6556
"steal-qunit": "^1.0.1",
66-
"steal-tools": "^1.2.0",
67-
"testee": "^0.7.0"
57+
"steal-tools": "^1.11.9",
58+
"testee": "^0.8.0"
6859
}
6960
}

0 commit comments

Comments
 (0)