-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix($http): JSON parse failure #15724
Changes from 1 commit
68ced16
573e0da
9d0c8b8
7deaae9
28cc76c
91393a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@ngdoc error | ||
@name $http:baddata | ||
@fullName Bad JSON Data | ||
@description | ||
|
||
This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object. | ||
`defaultHttpResponseTransform` expects the first parameter as a valid JSON object if the second parameter of headers specifies a Content-Type of JSON. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't mention |
||
|
||
The error message should provide additional context such as the actual value of the parameter that was received. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actual value of the parameter that was received --> actual response |
||
|
||
To resolve this error, make sure you pass a valid JSON data object to `defaultHttpResponseTransform`. | ||
|
||
For more information, see the {@link ng.$http `defaultHttpResponseTransform`} service API documentation. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,11 @@ function defaultHttpResponseTransform(data, headers) { | |
if (tempData) { | ||
var contentType = headers('Content-Type'); | ||
if ((contentType && (contentType.indexOf(APPLICATION_JSON) === 0)) || isJsonLike(tempData)) { | ||
data = fromJson(tempData); | ||
try { | ||
data = fromJson(tempData); | ||
} catch (e) { | ||
throw minErr('$http')('baddata', 'Data must be a valid JSON object. Received: {0}', data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please, embed the actual error message into the |
||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please limit lines to 100 chars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link should point tot a specific section (e.g.
ng.$httpProvider.defaults
,ng.$http.defaults
, orng$http#default-transformations
).BTW, I just noticed that
$httpProvider.defaults.transformRequest
and$httpProvider.defaults.transformResponse
are not documented. They should be.