Skip to content

Instantly share code, notes, and snippets.

@esedic
Created June 19, 2025 07:22
Show Gist options
  • Save esedic/a6814e2b08e67c9dd64c49423994a799 to your computer and use it in GitHub Desktop.
Save esedic/a6814e2b08e67c9dd64c49423994a799 to your computer and use it in GitHub Desktop.
Using Fetch API for Ajax Requests
// GET Request:
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Failed to fetch data', error));
// POST Request:
fetch('https://api.example.com/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({name: 'John', job: 'Developer'})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Failed to post data', error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment