let blogId = $("#blogId").text(); function loadComments() { if (document.location.pathname.startsWith("/Blog")) { fetch(`/api/Blog/${blogId}/comments`) .then(response => { let data = response.json(); data.then(k => { for (let i = 0; i < k.length; i++) { let date = new Date(k[i].dateTime * 1000); $("#commentBar").after( `
${k[i].creatorMail}

${date.toLocaleString()}

${k[i].text}

` ) } }); }); } } $("#postComment").click( function () { var contentBody = { text: $("#commentText").val(), sender: $("#userEmail").val() } $.ajax(`/api/Blog/${blogId}/comments`, { data: JSON.stringify(contentBody), contentType: "application/json", method: "post" }).done(response => { $("#commentText").val(''); $("#userEmail").val(''); $("#commentBar").empty(); loadComments(); }) } ); $(loadComments());