Вопрос 1
Вы разрабатываете форму регистрации. Нужно отправить данные на /api/users методом POST, корректно обработать сетевые и HTTP-ошибки (Hypertext Transfer Protocol) и получить JSON-Ответ: (JavaScript Object Notation). Какой фрагмент реализует требуемое поведение?
- 1fetch('/api/users', { method: 'POST', body: JSON.stringify(data)}).then(r =˃ r.json());.
- 2async function createUser(data) { try { const res = await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const json = await res.json(); return json; } catch (e) { console.error('Ошибка запроса:', e); throw e; }}.
- 3fetch('/api/users', { method: 'POST', body: data }) .then(r =˃ { if (!r.ok) return r; return JSON.parse(r); // парсинг не объекта Response }) .catch(console.error);.
- 4async function createUser(data) { const res = await fetch('/api/users'); const json = await res.json(); if (!json.success) throw new Error('fail'); return json;}.