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

Commit 0400dc9

Browse files
Narretzlgalfaso
authored andcommitted
docs($http): expand the param serializer docs
Closes #11745 Closes #12064
1 parent 71fc3f4 commit 0400dc9

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

src/ng/http.js

+57-13
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ function $HttpParamSerializerProvider() {
2323
* @name $httpParamSerializer
2424
* @description
2525
*
26-
* Default $http params serializer that converts objects to a part of a request URL
26+
* Default {@link $http `$http`} params serializer that converts objects to strings
2727
* according to the following rules:
28+
*
2829
* * `{'foo': 'bar'}` results in `foo=bar`
2930
* * `{'foo': Date.now()}` results in `foo=2015-04-01T09%3A50%3A49.262Z` (`toISOString()` and encoded representation of a Date object)
3031
* * `{'foo': ['bar', 'baz']}` results in `foo=bar&foo=baz` (repeated key for each array element)
3132
* * `{'foo': {'bar':'baz'}}` results in `foo=%7B%22bar%22%3A%22baz%22%7D"` (stringified and encoded representation of an object)
33+
*
34+
* Note that serializer will sort the request parameters alphabetically.
3235
* */
36+
3337
this.$get = function() {
3438
return function ngParamSerializer(params) {
3539
if (!params) return '';
@@ -56,7 +60,43 @@ function $HttpParamSerializerJQLikeProvider() {
5660
* @name $httpParamSerializerJQLike
5761
* @description
5862
*
59-
* Alternative $http params serializer that follows jQuery's [`param()`](http://api.jquery.com/jquery.param/) method logic.
63+
* Alternative {@link $http `$http`} params serializer that follows
64+
* jQuery's [`param()`](http://api.jquery.com/jquery.param/) method logic.
65+
* The serializer will also sort the params alphabetically.
66+
*
67+
* To use it for serializing `$http` request parameters, set it as the `paramSerializer` property:
68+
*
69+
* ```js
70+
* $http({
71+
* url: myUrl,
72+
* method: 'GET',
73+
* params: myParams,
74+
* paramSerializer: '$httpParamSerializerJQLike'
75+
* });
76+
* ```
77+
*
78+
* It is also possible to set it as the default `paramSerializer` in the
79+
* {@link $httpProvider#defaults `$httpProvider`}.
80+
*
81+
* Additionally, you can inject the serializer and use it explicitly, for example to serialize
82+
* form data for submission:
83+
*
84+
* ```js
85+
* .controller(function($http, $httpParamSerializerJQLike) {
86+
* //...
87+
*
88+
* $http({
89+
* url: myUrl,
90+
* method: 'POST',
91+
* data: $httpParamSerializerJQLike(myData),
92+
* headers: {
93+
* 'Content-Type': 'application/x-www-form-urlencoded'
94+
* }
95+
* });
96+
*
97+
* });
98+
* ```
99+
*
60100
* */
61101
this.$get = function() {
62102
return function jQueryLikeParamSerializer(params) {
@@ -230,10 +270,11 @@ function $HttpProvider() {
230270
* - **`defaults.headers.put`**
231271
* - **`defaults.headers.patch`**
232272
*
233-
* - **`defaults.paramSerializer`** - {string|function(Object<string,string>):string} - A function used to prepare string representation
234-
* of request parameters (specified as an object).
235-
* If specified as string, it is interpreted as a function registered with the {@link auto.$injector $injector}.
236-
* Defaults to {@link ng.$httpParamSerializer $httpParamSerializer}.
273+
*
274+
* - **`defaults.paramSerializer`** - `{string|function(Object<string,string>):string}` - A function
275+
* used to the prepare string representation of request parameters (specified as an object).
276+
* If specified as string, it is interpreted as a function registered with the {@link auto.$injector $injector}.
277+
* Defaults to {@link ng.$httpParamSerializer $httpParamSerializer}.
237278
*
238279
**/
239280
var defaults = this.defaults = {
@@ -708,9 +749,8 @@ function $HttpProvider() {
708749
*
709750
* - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc)
710751
* - **url** – `{string}` – Absolute or relative URL of the resource that is being requested.
711-
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned
712-
* to `?key1=value1&key2=value2` after the url. If the value is not a string, it will be
713-
* JSONified.
752+
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be serialized
753+
* with the `paramSerializer` and appended as GET parameters.
714754
* - **data** – `{string|Object}` – Data to be sent as the request message data.
715755
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
716756
* HTTP headers to send to the server. If the return value of a function is null, the
@@ -728,10 +768,14 @@ function $HttpProvider() {
728768
* transform function or an array of such functions. The transform function takes the http
729769
* response body, headers and status and returns its transformed (typically deserialized) version.
730770
* See {@link ng.$http#overriding-the-default-transformations-per-request
731-
* Overriding the Default Transformations}
732-
* - **paramSerializer** - {string|function(Object<string,string>):string} - A function used to prepare string representation
733-
* of request parameters (specified as an object).
734-
* Is specified as string, it is interpreted as function registered in with the {$injector}.
771+
* Overriding the Default TransformationjqLiks}
772+
* - **paramSerializer** - `{string|function(Object<string,string>):string}` - A function used to
773+
* prepare the string representation of request parameters (specified as an object).
774+
* If specified as string, it is interpreted as function registered with the
775+
* {@link $injector $injector}, which means you can create your own serializer
776+
* by registering it as a {@link auto.$provide#service service}.
777+
* The default serializer is the {@link $httpParamSerializer $httpParamSerializer};
778+
* alternatively, you can use the {@link $httpParamSerializerJQLike $httpParamSerializerJQLike}
735779
* - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
736780
* GET request, otherwise if a cache instance built with
737781
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for

0 commit comments

Comments
 (0)