以下のコードは、最初に以前のコメントを表示しようとしています。次に、送信ボタンをクリックすると、新しいコメントを表示して更新します。
2 番目の AJax 呼び出しは正常に機能します。しかし、最初の Ajax 呼び出しは何もしません (「/show」は有効な要求です。最初の呼び出し「/comment」は「/show」を呼び出します)。
ここに私のコードスニペットがあります:
<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>
前もって感謝します