Skip to content

Commit 68ced16

Browse files
committed
fix($http): JSON parse failure
Fixes angular#15695
1 parent b55637a commit 68ced16

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@ngdoc error
2+
@name $http:baddata
3+
@fullName Bad JSON Data
4+
@description
5+
6+
This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object.
7+
`defaultHttpResponseTransform` expects the first parameter as a valid JSON object if the second parameter of headers specifies a Content-Type of JSON.
8+
9+
The error message should provide additional context such as the actual value of the parameter that was received.
10+
11+
To resolve this error, make sure you pass a valid JSON data object to `defaultHttpResponseTransform`.
12+
13+
For more information, see the {@link ng.$http `defaultHttpResponseTransform`} service API documentation.

src/ng/http.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ function defaultHttpResponseTransform(data, headers) {
138138
if (tempData) {
139139
var contentType = headers('Content-Type');
140140
if ((contentType && (contentType.indexOf(APPLICATION_JSON) === 0)) || isJsonLike(tempData)) {
141-
data = fromJson(tempData);
141+
try {
142+
data = fromJson(tempData);
143+
} catch (e) {
144+
throw minErr('$http')('baddata', 'Data must be a valid JSON object. Received: {0}', data);
145+
}
142146
}
143147
}
144148
}

0 commit comments

Comments
 (0)