Javascript AJAX不会在聊天室系统中显示最后一条消息,除非页面刷新
我为一个网站制作了一个聊天框,其中显示了用户对他的姓名和日期的评论。除了jQueryAjax之外,它工作得很好。我尝试了所有方法,但在按下提交按钮后仍然看不到我的评论。它只在我刷新页面时出现Javascript AJAX不会在聊天室系统中显示最后一条消息,除非页面刷新,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我为一个网站制作了一个聊天框,其中显示了用户对他的姓名和日期的评论。除了jQueryAjax之外,它工作得很好。我尝试了所有方法,但在按下提交按钮后仍然看不到我的评论。它只在我刷新页面时出现 --> $(文档).ready(函数(){ $('comment_form')。('submit',函数(事件){ event.preventDefault(); var form_data=$(this).serialize(); $.ajax({ url:“https://manix.000we
-->
$(文档).ready(函数(){
$('comment_form')。('submit',函数(事件){
event.preventDefault();
var form_data=$(this).serialize();
$.ajax({
url:“https://manix.000webhostapp.com/community.php",
方法:“张贴”,
数据:表格数据,
数据类型:“JSON”,
成功:功能(数据){
如果(data.error!=''){
$('#注释形式'[0].reset();
$('#comment_message').html(data.error);
}
}
})
});
加载注释();
函数加载_注释(){
$.ajax(){
url:“https://manix.000webhostapp.com/community.php",
方法:“张贴”,
成功:功能(数据){
$('#display_comment').html(数据);
}
}
}
});
Ajax是异步的,因此当您调用load\u comment()时,注释仍然不存在
只需将load_注释放入ajax回调中
$(document).ready(function() {
$('#comment_form').('submit', function(event) {
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url: "https://manix.000webhostapp.com/community.php",
method: "POST",
data: form_data,
dataType: "JSON",
success: function(data) {
if (data.error != '') {
$('#comment_form')[0].reset();
$('#comment_message').html(data.error);
} else {
load_comment();
}
}
})
});
function load_comment() {
$.ajax() {
url: "https://manix.000webhostapp.com/community.php",
method: "POST",
success: function(data) {
$('#display_comment').html(data);
}
}
}
});
Ajax是异步的,因此当调用load_comment()时,注释仍然不存在 只需将load_注释放入ajax回调中
$(document).ready(function() {
$('#comment_form').('submit', function(event) {
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url: "https://manix.000webhostapp.com/community.php",
method: "POST",
data: form_data,
dataType: "JSON",
success: function(data) {
if (data.error != '') {
$('#comment_form')[0].reset();
$('#comment_message').html(data.error);
} else {
load_comment();
}
}
})
});
function load_comment() {
$.ajax() {
url: "https://manix.000webhostapp.com/community.php",
method: "POST",
success: function(data) {
$('#display_comment').html(data);
}
}
}
});
我只看到你给load_comment打过一次电话。每次需要加载注释时都需要调用它,例如在SubmitAjax回调中。或者,如果您用于发布注释的端点也返回注释,则使用
数据
变量更新那里的注释。这是因为您的成功
处理程序不使用刚刚添加的注释更新DOM…我如何使用display\u comment id和comment添加新的div,当提交按钮按下时,jquery ajax中的注释编写器和日期我只看到您调用load_comment一次。每次需要加载注释时都需要调用它,例如在SubmitAjax回调中。或者,如果您用于发布注释的端点也返回注释,则使用数据
变量更新那里的注释。这是因为您的成功
处理程序不使用刚刚添加的注释更新DOM…我如何使用display\u comment id和comment添加新的div,按下submit按钮时jQueryAjax中的注释编写器和日期