Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs($http): correct and clarify default transforms #15906

Merged
merged 2 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/content/error/$http/baddata.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
@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
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 valid JSON data to `transformResponse` or use an
appropriate `Content-Type` header for non-JSON data.
To resolve this error, make sure you pass valid JSON data to `transformResponse`. If the response
data looks like JSON, but has a different `Content-Type` header, you must
{@link ng.$http#overriding-the-default-transformations-per-request implement your own response
transformer on a per request basis}, or {@link ng.$http#default-transformations modify the default `$http` responseTransform}.
44 changes: 32 additions & 12 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,6 @@ function $HttpProvider() {
* {@link ng.$cacheFactory `$cacheFactory`} to enable or disable caching of HTTP responses
* by default. See {@link $http#caching $http Caching} for more information.
*
* - **`defaults.xsrfCookieName`** - {string} - Name of cookie containing the XSRF token.
* Defaults value is `'XSRF-TOKEN'`.
*
* - **`defaults.xsrfHeaderName`** - {string} - Name of HTTP header to populate with the
* XSRF token. Defaults value is `'X-XSRF-TOKEN'`.
*
* - **`defaults.headers`** - {Object} - Default headers for all $http requests.
* Refer to {@link ng.$http#setting-http-headers $http} for documentation on
* setting default headers.
Expand All @@ -280,15 +274,38 @@ function $HttpProvider() {
* - **`defaults.headers.put`**
* - **`defaults.headers.patch`**
*
* - **`defaults.jsonpCallbackParam`** - `{string}` - the name of the query parameter that passes the name of the
* callback in a JSONP request. The value of this parameter will be replaced with the expression generated by the
* {@link $jsonpCallbacks} service. Defaults to `'callback'`.
*
* - **`defaults.paramSerializer`** - `{string|function(Object<string,string>):string}` - A function
* used to the prepare string representation of request parameters (specified as an object).
* If specified as string, it is interpreted as a function registered with the {@link auto.$injector $injector}.
* Defaults to {@link ng.$httpParamSerializer $httpParamSerializer}.
*
* - **`defaults.jsonpCallbackParam`** - `{string}` - the name of the query parameter that passes the name of the
* callback in a JSONP request. The value of this parameter will be replaced with the expression generated by the
* {@link $jsonpCallbacks} service. Defaults to `'callback'`.
* - **`defaults.transformRequest`** -
* `{Array<function(data, headersGetter)>|function(data, headersGetter)}` -
* An array of functions (or a single function) which are applied to the request data.
* By default, this is an array with one request transformation function:
*
* - If the `data` property of the request configuration object contains an object, serialize it
* into JSON format.
*
* - **`defaults.transformResponse`** -
* `{Array<function(data, headersGetter, status)>|function(data, headersGetter, status)}` -
* An array of functions (or a single function) which are applied to the response data. By default,
* this is an array which applies one response transformation function that does two things:
*
* - If XSRF prefix is detected, strip it
* (see {@link ng.$http#security-considerations Security Considerations in the $http docs}).
* - If the `Content-Type` is `application/json` or the response looks like JSON,
* deserialize it using a JSON parser.
*
* - **`defaults.xsrfCookieName`** - {string} - Name of cookie containing the XSRF token.
* Defaults value is `'XSRF-TOKEN'`.
*
* - **`defaults.xsrfHeaderName`** - {string} - Name of HTTP header to populate with the
* XSRF token. Defaults value is `'X-XSRF-TOKEN'`.
*
**/
var defaults = this.defaults = {
Expand Down Expand Up @@ -552,15 +569,18 @@ function $HttpProvider() {
*
* AngularJS provides the following default transformations:
*
* Request transformations (`$httpProvider.defaults.transformRequest` and `$http.defaults.transformRequest`):
* Request transformations (`$httpProvider.defaults.transformRequest` and `$http.defaults.transformRequest`) is
* an array with one function that does the following:
*
* - If the `data` property of the request configuration object contains an object, serialize it
* into JSON format.
*
* Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`):
* Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`) is
* an array with one function that does the following:
*
* - If XSRF prefix is detected, strip it (see Security Considerations section below).
* - If JSON response is detected, deserialize it using a JSON parser.
* - If the `Content-Type` is `application/json` or the response looks like JSON,
* deserialize it using a JSON parser.
*
*
* ### Overriding the Default Transformations Per Request
Expand Down