fix(common): attempt to JSON.parse errors for JSON responses (#19773)

PR Close #19773
This commit is contained in:
Alex Rickabaugh
2017-10-17 17:30:56 -07:00
committed by Tobias Bosch
parent 25cbc98979
commit 04ab9f1917
2 changed files with 16 additions and 1 deletions

View File

@ -191,6 +191,14 @@ export class HttpXhrBackend implements HttpBackend {
// The parse error contains the text of the body that failed to parse.
body = { error, text: body } as HttpJsonParseError;
}
} else if (!ok && req.responseType === 'json' && typeof body === 'string') {
try {
// Attempt to parse the body as JSON.
body = JSON.parse(body);
} catch (error) {
// Cannot be certain that the body was meant to be parsed as JSON.
// Leave the body as a string.
}
}
if (ok) {