Skip to content

Commit 7deaae9

Browse files
committed
fix($http): JSON parse failure
Fixes angular#15695
1 parent 9d0c8b8 commit 7deaae9

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

docs/content/error/$http/baddata.ngdoc

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
@fullName Bad JSON Data
44
@description
55

6-
The default @{link ng$http#default-transformations `transformResponse`} will try to parse the
6+
The default @{link ng.$http#default-transformations `transformResponse`} will try to parse the
77
response as JSON if the `Content-Type` header is `application/json` or the response looks like a
88
valid JSON-stringified object or array.
99
This error occurs when that data is not a valid JSON object.
1010

1111
The error message should provide additional context such as the actual response.
1212

13-
To resolve this error, make sure you pass a valid JSON data object to `transformResponse`.
14-
15-
For more information, see the {@link ng$http#default-transformations `transformResponse`} service
16-
API documentation.
13+
To resolve this error, make sure you pass valid JSON data to `transformResponse` or use an
14+
appropriate `Content-Type` header for non-JSON data.

src/ng/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function defaultHttpResponseTransform(data, headers) {
142142
data = fromJson(tempData);
143143
} catch (e) {
144144
throw $httpMinErr('baddata', 'Data must be a valid JSON object. Received: "{0}". ' +
145-
'Error occurred: "{1}"', data, e);
145+
'Parse error: "{1}"', data, e);
146146
}
147147
}
148148
}

test/ng/httpSpec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1446,12 +1446,12 @@ describe('$http', function() {
14461446
it('should return JSON data with error message if JSON is invalid', function() {
14471447
var errCallback = jasmine.createSpy('error');
14481448
$httpBackend.expect('GET', '/url').respond('{abcd}', {'Content-Type': 'application/json'});
1449-
$http({method: 'GET', url: '/url'}).then(callback).catch(errCallback);
1450-
$httpBackend.flush();
1449+
$http.get('/url').then(callback).catch(errCallback);
1450+
$httpBackend.flush();
14511451

1452-
expect(callback).not.toHaveBeenCalled();
1453-
expect(errCallback).toHaveBeenCalledOnce();
1454-
expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr('$http', 'baddata');
1452+
expect(callback).not.toHaveBeenCalled();
1453+
expect(errCallback).toHaveBeenCalledOnce();
1454+
expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr('$http', 'baddata');
14551455
});
14561456
});
14571457
});

0 commit comments

Comments
 (0)