Skip to content

Commit be4a38b

Browse files
authored
Merge pull request #47 from ivospinheiro/ISSUE_46
Handle error thrown when parsing responseText as JSON
2 parents a36ffc1 + 1c97fcf commit be4a38b

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

can-ajax-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,42 @@ QUnit.test("handles 204 No Content responses when expecting JSON", function (ass
452452
});
453453
});
454454

455+
QUnit.test("handles responseText containing text when expecting JSON (#46)", function (assert) {
456+
var done = assert.async();
457+
var NOT_FOUND_CODE = 404;
458+
var NOT_FOUND_MSG = "NOT FOUND";
459+
var headers = {},
460+
restore = makeFixture(function () {
461+
this.open = function (type, url) {};
462+
463+
this.send = function () {
464+
this.readyState = 4;
465+
this.status = NOT_FOUND_CODE;
466+
this.responseText = NOT_FOUND_MSG;
467+
this.onreadystatechange();
468+
};
469+
470+
this.setRequestHeader = function (header, value) {
471+
headers[header] = value;
472+
};
473+
});
474+
475+
ajax({
476+
type: "get",
477+
url: "/foo",
478+
dataType: "json"
479+
}).then(function (value) {
480+
assert.notOk(value, "success callback call not expected");
481+
}, function (xhr) {
482+
assert.equal(xhr.status, 404);
483+
assert.equal(xhr.responseText, NOT_FOUND_MSG);
484+
}).then(function () {
485+
// restore original values
486+
restore();
487+
done();
488+
});
489+
});
490+
455491
if (hasLocalServer) {
456492
QUnit.test("correctly serializes null and undefined values (#177)", function (assert) {
457493
var done = assert.async();

can-ajax.js

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

104104
var _xhrResp = function (xhr, options) {
105-
var type = (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]);
106-
107-
if(type && (xhr.responseText || xhr.responseXML)){
105+
try{
106+
var type = (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]);
108107

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;
108+
if(type && (xhr.responseText || xhr.responseXML)){
109+
110+
switch (type) {
111+
case "text/xml":
112+
case "xml":
113+
return xhr.responseXML;
114+
case "text/json":
115+
case "application/json":
116+
case "text/javascript":
117+
case "application/javascript":
118+
case "application/x-javascript":
119+
case "json":
120+
return xhr.responseText && JSON.parse(xhr.responseText);
121+
default:
122+
return xhr.responseText;
123+
}
124+
} else {
125+
return xhr;
122126
}
123-
} else {
127+
} catch(e){
124128
return xhr;
125129
}
126130
};

0 commit comments

Comments
 (0)