Skip to content

Commit c599796

Browse files
author
Julian Kern
committed
Merge branch 'json-response' into major
# Conflicts: # can-ajax.js
2 parents 63461d6 + 44315e4 commit c599796

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

can-ajax.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,26 @@ var contentTypes = {
102102
};
103103

104104
var _xhrResp = function (xhr, options) {
105-
switch (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]) {
106-
case "text/xml":
107-
case "xml":
108-
return xhr.responseXML;
109-
case "text/json":
110-
case "application/json":
111-
case "text/javascript":
112-
case "application/javascript":
113-
case "application/x-javascript":
114-
case "json":
115-
return xhr.responseText && JSON.parse(xhr.responseText);
116-
default:
117-
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;
118125
}
119126
};
120127

@@ -187,7 +194,7 @@ function ajax(o) {
187194
if (xhr.status >= 200 && xhr.status < 300) {
188195
deferred.resolve( _xhrResp(xhr, o) );
189196
} else {
190-
deferred.reject( xhr );
197+
deferred.reject( _xhrResp(xhr, o) );
191198
}
192199
}
193200
else if (o.progress) {

0 commit comments

Comments
 (0)