how to make a fetch req in js
- User
how to make a fetch req in js
Assistant
Here are a few ways to make a basic fetch request in JavaScript:
Using fetch:
fetch('https://example.com/movies.json') .then(response => response.json()) .then(data => console.log(data));Using async/await:
async function getMovies() { const response = await fetch('https://example.com/movies.json'); const data = await response.json(); console.log(data); }With options object:
fetch('https://example.com/movies.json', { method: 'GET' }) .then(response => response.json()) .then(data => console.log(data));Post request:
fetch('https://example.com/movies', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'A New Hope' }) }) .then(response => response.json()) .then(data => console.log(data));
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.