codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS fetch API

JS fetch API

Request data from a server.

fetch(url) returns a promise. Check response.ok, then call json(). Network access is blocked in this preview, so the example handles the failure; it works on your own site.

Example code

<p id='o'>Loading...</p>
<script>
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(r=>{if(!r.ok)throw new Error(r.status);return r.json();})
.then(d=>o.textContent=d.title)
.catch(e=>o.textContent='Request failed (blocked here): '+e.message);
</script>

Run this code in the online editor

← JS window APIJS JSON →