-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfetchexample.js
More file actions
45 lines (41 loc) · 1.45 KB
/
Copy pathfetchexample.js
File metadata and controls
45 lines (41 loc) · 1.45 KB
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
41
42
43
44
45
//see https://fetch.spec.whatwg.org/#dom-global-fetch
const headers = new Headers({ [`Content-Type`]: `application/json` }); //can have more
const input = `something.json`; //input can be an URL string or Request instance
//dont forget , if you remove some comments
const options = {
method: `GET`, //list of safe methods https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html,
headers: headers,
body: `` /*//Blob or BufferSource or FormData or URLSearchParams or USVString
referrer: ``,//string
referrerPolicy: ``,
//https://w3c.github.io/webappsec-referrer-policy/#enumdef-referrerpolicy
mode: RequestMode,
credentials: RequestCredentials,
cache: RequestCache,
redirect: RequestRedirect,
integrity: String,
keepalive: false,
*/
};
//options is optional
fetch(input, options).then(function (response) {
/*
most properties are read only, can use response.clone() or new Response()
response.url // String
response.redirected; // boolean
response.status; // short Number
response.ok; // boolean
response.statusText; // String
response.headers; // Headers
response.body; // null or ReadableStream;
the following return Promises:
response.text();
response.arrayBuffer();
response.blob();
response.formData();
response.json();
*/
return response.text();
}).then(function (text) {
//...
});