The code below is attempting to first display previous comments, and then on submit button click, refresh by showing the newer comment.
The second AJax call works fine. But the first Ajax call does nothing ('/show' is a valid request - and the first call '/comment' calls '/show')
Here is my code snippet:
<script>
$(document).ready(function() {
var source = $("#login_template");
var srcHTML =source.html();
var login_template = Handlebars.compile(srcHTML);
source = $("#comment_template");
srcHTML =source.html();
var comment_template = Handlebars.compile(srcHTML);
// First time call to server to get the comments
$.ajax({ // ajax call starts
type: "post",
url: "/show",
contentType: 'application/json',
success: function(data,textStatus,jqXHR)
{
data = JSON.parse(data);
var html = login_template(data);
$("#login_content").html(html);
html = comment_template(data);
$("#comment_content").html(html);
}
});
$("#comment_button").click(function(event) {
$.ajax({ // ajax call to send comment to server and refresh with newer comment
type: "post",
url: "/comment",
contentType: 'application/json',
data: JSON.stringify(
{
'source' : source.value,
'comment': comment.value,
}),
success: function(data,textStatus,jqXHR)
{
data = JSON.parse(data);
var html = login_template(data);
$("#login_content").html(html);
html = comment_template(data);
$("#comment_content").html(html);
}
});
});
});
</script>
Thanks in advance