1//Obj of data to send in future like a dummyDb
2const data = { username: 'example' };
3
4//POST request with body equal on data in JSON format
5fetch('https://example.com/profile', {
6 method: 'POST',
7 headers: {
8 'Content-Type': 'application/json',
9 },
10 body: JSON.stringify(data),
11})
12.then((response) => response.json())
13//Then with the data from the response in JSON...
14.then((data) => {
15 console.log('Success:', data);
16})
17//Then with the error genereted...
18.catch((error) => {
19 console.error('Error:', error);
20});
21
22// Yeah
1fetch('./yourjson.json')
2 .then((response) => response.json())
3 .then((data) => {
4 console.log(data);
5 })
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((myJson) => {
6 console.log(myJson);
7 });
1const _fetch = async props => {
2 const { endpoint, options } = props
3 return await fetch(endpoint, options)
4 .then(async res => {
5 if (res.ok) {
6 return await res.clone().json().catch(() => res.text())
7 }
8 return false
9 })
10 .catch(err => {
11 console.error("api", "_fetch", "err", err)
12 return false
13 })
14}
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((data) => {
6 console.log(data);
7 });
8
1var myHeaders = new Headers();
2
3var myInit = { method: 'POST',
4 headers: myHeaders,
5 mode: 'cors',
6 cache: 'default' };
7
8fetch('flowers.jpg',myInit)
9.then(function(response) {
10 return response.blob();
11})
12.then(function(myBlob) {
13 var objectURL = URL.createObjectURL(myBlob);
14 myImage.src = objectURL;
15});
16
17