I wanted help to get data from post method using fetch api? I tried the below code but can anyone help me understand how to work on it?
The script.js file as below,
//select the elements
const submitBtn = document.getElementById("submit");
//get data from the form
const formData = submitBtn.addEventListener('click', function (info) {
fetch(info)
.then((response) => {
console.log(response);
return response.json();
})
.then((data) => {
let datas = data;
datas.map(function (data) {
const node = document.createElement('li');
const textNode = document.createTextNode(`${author.blog}`);
node.appendChild(textNode);
console.log(node);
})
})
.catch(function (error) {
console.log(error);
})
});
HTML in short:
<form method="post">
<button type="submit" onSubmit="formData" class="btn btn-primary" id="submit">Submit</button>