-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix($http): JSON parse failure #15724
Changes from 2 commits
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,16 @@ | ||
@ngdoc error | ||
@name $http:baddata | ||
@fullName Bad JSON Data | ||
@description | ||
|
||
The default @{link ng$http#default-transformations `transformResponse`} will try to parse the | ||
response as JSON if the `Content-Type` header is `application/json` or the response looks like a | ||
valid JSON-stringified object or array. | ||
This error occurs when that data is not a valid JSON object. | ||
|
||
The error message should provide additional context such as the actual response. | ||
|
||
To resolve this error, make sure you pass a valid JSON data object to `transformResponse`. | ||
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. a valid JSON data object --> valid JSON data Also, the correct resolution depends on the source of the problem. In some cases, one might need to send correct headers (to not include 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. How about To resolve this error, make sure you pass valid JSON data to 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. How about:
|
||
|
||
For more information, see the {@link ng$http#default-transformations `transformResponse`} service | ||
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. This is not necessary, since we already link to |
||
API documentation. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,12 @@ 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 $httpMinErr('baddata', 'Data must be a valid JSON object. Received: "{0}". ' + | ||
'Error occurred: "{1}"', data, e); | ||
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. Maybe: Error occurred --> Original error or Parse error |
||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1442,6 +1442,17 @@ describe('$http', function() { | |
expect(callback.calls.argsFor(1)[0]).toBe(null); | ||
expect(callback.calls.argsFor(2)[0]).toBe(''); | ||
}); | ||
|
||
it('should return JSON data with error message if JSON is invalid', function() { | ||
var errCallback = jasmine.createSpy('error'); | ||
$httpBackend.expect('GET', '/url').respond('{abcd}', {'Content-Type': 'application/json'}); | ||
$http({method: 'GET', url: '/url'}).then(callback).catch(errCallback); | ||
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. Wrong indentation. Nit: It is simpler to use |
||
$httpBackend.flush(); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
expect(errCallback).toHaveBeenCalledOnce(); | ||
expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr('$http', 'baddata'); | ||
}); | ||
}); | ||
}); | ||
|
||
|
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.
Shouldn't this be
ng.$http#...
(with a dot afterng
)?