Не могу понять как правильно сформировать GET запрос
У меня есть таблица
<table id="info">
<tr>
<th>№ п/п</th>
<th>Номер</th>
<th>Название</th>
<th>Адрес</th>
<th>ФИО</th>
<th>Номер телефона директора</th>
</tr>
<tr>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>Пример названия</p></td>
<td><p>Пример адреса</p></td>
<td><p>ФИО директора</p></td>
<td><p>+00000000000</p></td>
</tr>
</table>
Под ней кнопка которая вызывает форму, в форму есть кнопка
<button type="submit" name = "submit" id="contact">Отправить в БД</button>
Она отправляет данные из формы в БД(методом POST) и возвращает берёт данные из JSON, который находиться по ссылке http://что-то:3000/info
// Implementation DRY principe, write repeated code into function
const httpRequest = (url, options) => fetch(url, options)
.then(response => response.json())
.then(console.log)
.catch(console.error);
// Create post request options
const postOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
// Create get request options
const getOptions = {
method: 'get',
headers: {
'Content-Type': 'application/json',
}
};
// Create post request function
const postRequest = data => httpRequest('http://что-то:3000/info', {
...postOptions,
body: JSON.stringify(data)
});
// Create get request function
const getRequest = _ => fetch('http://что-то:3000/info', getOptions)
.then(response => response.json())
.then(console.log)
.catch(console.error);
// Create form handler
const formHandler = (e) => {
// End handler after request
e.preventDefault();
// Get data from form
const data = Object.fromEntries(new FormData(e.target).entries());
// Send post request
postRequest(data);
// Send get request
getRequest();
form.classList.remove('open');
popup.classList.remove('popup_open');
}
// Set form handler
form.addEventListener('submit', formHandler);
И всё работает и всё хорошо, только вот я хотел не много другого. Я хотел что бы при GET-запросе данные которые идут при нём выводились в новые строки уже существующей таблицы. Я даже придумал примерный алгоритм
for (let i = 0; i < info.length; i++){
const row = document.createElement('tr');
for (let j =0; j < info[i].length; j++){
const cell = i === 0 ? document.createElement('th') : document.createEvent('td');
cell.textContent = info[i][j];
row.appendChild(cell);
}
info.append(row);
}
И я пытался это "вставить" вместо строки
.then(console.log)
точнее вместо значения console.log
, но получаю кучу ошибок из этого я сделал вывод что не туда это нужно "вставлять", только вот не пойму, а куда нужно ?