fetch javscript code example
Example 1: fetch api javascript
fetch('http://example.com/songs')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
Example 2: .fetch method
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
Example 3: fetch api in js
var myData = async () => {
try {
const raw_response = await fetch("https://jsonplaceholder.typicode.com/users");
if (!raw_response.ok) {
throw new Error(raw_response.status);
}
const json_data = await raw_response.json();
console.log(json_data);
}
catch (error) {
console.log(error);
}
}
fetchUsers();