Merge pull request #9 from smokku/nonJSON__response

Fixed uncaught exception on parsing non-JSON AJAX response
This commit is contained in:
Christian Alfoni 2015-01-16 17:29:33 +01:00
commit 7163b30246
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);
}
}