Код IT
← Каталог

Асинхронное программирование в JavaScript — fetch API

Фрагмент из «Асинхронное программирование в JavaScript»: fetch API.

JavaScript main.js
fetch('/api/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    title: 'Мой пост',
    content: 'Привет, мир!'
  })
})
.then(response => response.json())
.then(newPost => {
  console.log('Создано:', newPost);
});
fetch('/api/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    title: 'Мой пост',
    content: 'Привет, мир!'
  })
})
.then(response => response.json())
.then(newPost => {
  console.log('Создано:', newPost);
});