Fixed uncaught exception on parsing non-JSON AJAX response

This commit is contained in:
Tomasz Sterna 2015-01-16 16:00:48 +01:00
parent b8c4414976
commit 24b9110a62
1 changed files with 9 additions and 4 deletions

View File

@ -64,10 +64,15 @@ var request = function (method, url, data, contentType, headers) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.responseText ? JSON.parse(xhr.responseText) : null);
} else {
reject(xhr.responseText ? JSON.parse(xhr.responseText) : null);
try {
var response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
if (xhr.status >= 200 && xhr.status < 300) {
resolve(response);
} else {
reject(response);
}
} catch (e) {
reject(e);
}
}