-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpPost.ts
40 lines (39 loc) · 1.05 KB
/
httpPost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Makes a `POST` request to the passed URL.
* @param url - URL to send the request to.
* @param data - Data to send.
* @param callback - Callback to invoke when the request completes.
* @param err - Callback to invoke when an error occurs.
* @example
* ```typescript
* 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
* );
* ```
* @public
* @since 0.1.3
* @category BOM
*/
const httpPost = (
url: string | URL,
data: string,
callback: (responseText: string) => void,
err: (request: XMLHttpRequest, event: ProgressEvent) => void = 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 = (event) => err(request, event);
request.send(data);
};
export default httpPost;