Comment on page

Post

function postData() {
// Use the Fetch API to make a POST request to a specific URL
fetch('https://jsonplaceholder.typicode.com/todos/', {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => {
// Check if the response was OK
if (!response.ok) {
// If not, throw an error
throw new Error('Network response was not OK');
}
// If the response was OK, parse the response as JSON
return response.json();
})
.then((data) => {
// Log the response
console.log('Response', data)
})
.catch((error) => {
// Log any network errors that occur
console.error('There has been a problem with your fetch operation:', error);
});
}
Last modified 10mo ago