1
0
mirror of https://github.com/flarum/core.git synced 2025-02-25 11:43:19 +01:00

Only parse as JSON if appropriate content type

This commit is contained in:
Toby Zerner 2015-10-29 17:52:18 +10:30
parent 7127bea15e
commit e1315d27a4

View File

@ -202,13 +202,7 @@ export default class App {
// When we deserialize JSON data, if for some reason the server has provided // When we deserialize JSON data, if for some reason the server has provided
// a dud response, we don't want the application to crash. We'll show an // a dud response, we don't want the application to crash. We'll show an
// error message to the user instead. // error message to the user instead.
options.deserialize = options.deserialize || (responseText => { options.deserialize = options.deserialize || (responseText => responseText);
try {
return JSON.parse(responseText);
} catch (e) {
throw new RequestError(500, responseText, options);
}
});
options.errorHandler = options.errorHandler || (error => { options.errorHandler = options.errorHandler || (error => {
throw error; throw error;
@ -233,7 +227,13 @@ export default class App {
throw new RequestError(status, responseText, options, xhr); throw new RequestError(status, responseText, options, xhr);
} }
return responseText; if (xhr.getResponseHeader('content-type').indexOf('json') !== -1) {
try {
return JSON.parse(responseText);
} catch (e) {
throw new RequestError(500, responseText, options, xhr);
}
}
}; };
if (this.requestError) this.alerts.dismiss(this.requestError.alert); if (this.requestError) this.alerts.dismiss(this.requestError.alert);