I’m writing code in express.js. Here I want to reload a div on the webhook response.
Here is a sample piece of my code.
server.js
router.post("/reviews", async (req, res) => {
console.log(req.body.review.comments);
});
This block triggers perfectly whenever there is an action happening at my webhook.
I use the below HTML to post the data and whenever there is a response, I want to refresh it(Getting data on refresh is handled and is working fine, tested this when I am posting data).
sample.html
<div id="reviewsWrapper">
<div class="reviewsContainer" id="reviewsContainer">
New Review Received
</div>
</div>
<script>
var mainForm = document.querySelector('#form');
mainForm.addEventListener("submit", handleFormSubmit);
async function handleFormSubmit(event) {
event.preventDefault();
const form = event.currentTarget;
const url = form.action;
try {
const formData = new FormData(form);
const data = JSON.stringify(Object.fromEntries(formData));
const config = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: data,
};
const rawResponse = await fetch(url, config);
const content = await rawResponse.json();
if (content)
$('#reviewsWrapper').load(location.href + ' #reviewsWrapper>*')
} catch (error) {
console.error(error);
}
}
</script>
I want to understand, whenever I have a new action in my webhook, how can I refresh my div(reviewsWrapper
)?
Thanks