Nest all content into snippets
This commit is contained in:
58
snippets/js/s/http-post.md
Normal file
58
snippets/js/s/http-post.md
Normal file
@ -0,0 +1,58 @@
|
||||
---
|
||||
title: HTTP post
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [browser]
|
||||
cover: boats
|
||||
dateModified: 2020-10-19T22:49:51+03:00
|
||||
---
|
||||
|
||||
Makes a `POST` request to the passed URL.
|
||||
|
||||
- Use the `XMLHttpRequest` web API to make a `POST` request to the given `url`.
|
||||
- Set the value of an `HTTP` request header with `setRequestHeader` method.
|
||||
- Handle the `onload` event, by calling the given `callback` the `responseText`.
|
||||
- Handle the `onerror` event, by running the provided `err` function.
|
||||
- Omit the fourth argument, `err`, to log errors to the console's `error` stream by default.
|
||||
|
||||
```js
|
||||
const httpPost = (url, data, callback, err = console.error) => {
|
||||
const request = new XMLHttpRequest();
|
||||
request.open('POST', url, true);
|
||||
request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
|
||||
request.onload = () => callback(request.responseText);
|
||||
request.onerror = () => err(request);
|
||||
request.send(data);
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
const newPost = {
|
||||
userId: 1,
|
||||
id: 1337,
|
||||
title: 'Foo',
|
||||
body: 'bar bar bar'
|
||||
};
|
||||
const data = JSON.stringify(newPost);
|
||||
httpPost(
|
||||
'https://jsonplaceholder.typicode.com/posts',
|
||||
data,
|
||||
console.log
|
||||
); /*
|
||||
Logs: {
|
||||
"userId": 1,
|
||||
"id": 1337,
|
||||
"title": "Foo",
|
||||
"body": "bar bar bar"
|
||||
}
|
||||
*/
|
||||
httpPost(
|
||||
'https://jsonplaceholder.typicode.com/posts',
|
||||
null, // does not send a body
|
||||
console.log
|
||||
); /*
|
||||
Logs: {
|
||||
"id": 101
|
||||
}
|
||||
*/
|
||||
```
|
||||
Reference in New Issue
Block a user