1fetch('http://example.com/songs')
2 .then(response => response.json())
3 .then(data => console.log(data))
4 .catch(err => console.error(err));
1fetch('https://api.github.com/users/manishmshiva', {
2 method: "GET",
3 headers: {"Content-type": "application/json;charset=UTF-8"}
4})
5.then(response => response.json())
6.then(json => console.log(json));
7.catch(err => console.log(err));
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((myJson) => {
6 console.log(myJson);
7 });
1const data = { username: 'example' };
2
3fetch('https://example.com/profile', {
4 method: 'POST', // or 'PUT'
5 headers: {
6 'Content-Type': 'application/json',
7 },
8 body: JSON.stringify(data),
9})
10.then(response => response.json())
11.then(data => {
12 console.log('Success:', data);
13})
14.catch((error) => {
15 console.error('Error:', error);
16});
17
1// fetch API
2var myData = async () => {
3 try {
4 const raw_response = await fetch("https://jsonplaceholder.typicode.com/users");
5 if (!raw_response.ok) { // check for the 404 errors
6 throw new Error(raw_response.status);
7 }
8 const json_data = await raw_response.json();
9 console.log(json_data);
10 }
11 catch (error) { // catch block for network errors
12 console.log(error);
13 }
14}
15fetchUsers();
1// GET Request.
2fetch('https://jsonplaceholder.typicode.com/users')
3 // Handle success
4 .then(response => response.json()) // convert to json
5 .then(json => console.log(json)) //print data to console
6 .catch(err => console.log('Request Failed', err)); // Catch errors