mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Emphasize the URLSearchParam built-in interface (#4590)
Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -801,20 +801,17 @@ cancel();
|
|||||||
|
|
||||||
## Using application/x-www-form-urlencoded format
|
## Using application/x-www-form-urlencoded format
|
||||||
|
|
||||||
By default, axios serializes JavaScript objects to `JSON`. To send data in the `application/x-www-form-urlencoded` format instead, you can use one of the following options.
|
By default, axios serializes JavaScript objects to `JSON`. To send data in the [`application/x-www-form-urlencoded` format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) instead, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API, which is [supported](http://www.caniuse.com/#feat=urlsearchparams) in the vast majority of browsers, [and Node](https://nodejs.org/api/url.html#url_class_urlsearchparams) starting with v10 (released in 2018).
|
||||||
|
|
||||||
### Browser
|
|
||||||
|
|
||||||
In a browser, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API as follows:
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams({ foo: 'bar' });
|
||||||
params.append('param1', 'value1');
|
params.append('extraparam', 'value');
|
||||||
params.append('param2', 'value2');
|
|
||||||
axios.post('/foo', params);
|
axios.post('/foo', params);
|
||||||
```
|
```
|
||||||
|
|
||||||
> Note that `URLSearchParams` is not supported by all browsers (see [caniuse.com](http://www.caniuse.com/#feat=urlsearchparams)), but there is a [polyfill](https://github.com/WebReflection/url-search-params) available (make sure to polyfill the global environment).
|
### Older browsers
|
||||||
|
|
||||||
|
For compatibility with very old browsers, there is a [polyfill](https://github.com/WebReflection/url-search-params) available (make sure to polyfill the global environment).
|
||||||
|
|
||||||
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
|
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
|
||||||
|
|
||||||
@@ -837,25 +834,15 @@ const options = {
|
|||||||
axios(options);
|
axios(options);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Node.js
|
### Older Node.js versions
|
||||||
|
|
||||||
#### Query string
|
For older Node.js engines, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
|
||||||
|
|
||||||
In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
|
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
|
||||||
```
|
```
|
||||||
|
|
||||||
or ['URLSearchParams'](https://nodejs.org/api/url.html#url_class_urlsearchparams) from ['url module'](https://nodejs.org/api/url.html) as follows:
|
|
||||||
|
|
||||||
```js
|
|
||||||
const url = require('url');
|
|
||||||
const params = new url.URLSearchParams({ foo: 'bar' });
|
|
||||||
axios.post('http://something.com/', params.toString());
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also use the [`qs`](https://github.com/ljharb/qs) library.
|
You can also use the [`qs`](https://github.com/ljharb/qs) library.
|
||||||
|
|
||||||
> NOTE:
|
> NOTE:
|
||||||
|
|||||||
Reference in New Issue
Block a user