Javascript 如何更新Ajax调用(而不是内容)

Javascript 如何更新Ajax调用(而不是内容),javascript,jquery,Javascript,Jquery,请看这段代码——在内容已经被调用之后,我如何终止/更新或重新启动ajax调用(而不是ajax调用的内容) 我的意思是,$(“#posting_main”)被称为onclick并设置动画-如何停止ajax并在另一次单击时使其成为另一个$(“#posting_main”) $(document).ready(function() { $("#img_x_ok").click(function(e){ e.preventDefault(); var post_text = $.trim($

请看这段代码——在内容已经被调用之后,我如何终止/更新或重新启动ajax调用(而不是ajax调用的内容)

我的意思是,
$(“#posting_main”)
被称为
onclick
并设置动画-如何停止ajax并在另一次单击时使其成为另一个
$(“#posting_main”)

 $(document).ready(function() {

 $("#img_x_ok").click(function(e){
 e.preventDefault();

 var post_text = $.trim($("#main_text_area").val());

 var data_text = 'post_text='+ post_text;
 if (post_text === "") return;

 var xhr = $.ajax({

 type: "POST",
 url: "comm_main_post.php",
 data: data_text,
 cache: false,

 success: function (data){
 //content

 $("#posting_main").fadeIn();
 $("#posting_main").load("pull_comm.php");
 $("#main_text_area").attr("value", "");

 $("#posting_main").animate({ 
     marginTop: "+=130px",
 }, 1000 );

 }

 }); //ajax close

 }); }); //both functions close
您可以使用:

完成后,您可以运行另一个
$.ajax(…)
来发出第二个请求


您可以像下面这样实现它。请注意,缩进代码使其更具可读性

 $(document).ready(function() {

     var xhr; // by placing it outside the click handler, you don't create
              // a new xhr each time. Rather, you can access the previous xhr
              // and overwrite it this way

     $("#img_x_ok").click(function(e){

         e.preventDefault();

         var post_text = $.trim($("#main_text_area").val());

         var data_text = 'post_text='+ post_text;
         if (post_text === "") return;

         if(xhr) xhr.abort(); // abort current xhr if there is one

         xhr = $.ajax({

             type: "POST",
             url: "comm_main_post.php",
             data: data_text,
             cache: false,

             success: function (data){
                 //content

                 $("#posting_main").fadeIn();
                 $("#posting_main").load("pull_comm.php");
                 $("#main_text_area").attr("value", "");

                 $("#posting_main").animate({ 
                     marginTop: "+=130px",
                 }, 1000 );

             }

         });

     });

});

但是,我不确定我是否完全理解你的问题:

  • abort()将终止AJAX请求。调用abort()后,如果需要,可以修改并重新发送请求
  • $(“#posting_main”).stop()将停止fadeIn动画。(我认为您可能需要使用$(“#posting_main”).hide()来跟踪它,以确保它不会部分可见。)

  • 您好……您能更具体地说明我应该将xhr.abort()放在哪里吗;我已经做过了,但是在这种情况下,Ajax不会在第一次调用时运行……我想我们需要知道您希望Ajax调用停止的时间和原因,以完全回答您的问题。这是对点击等事件的响应吗?@pimvdb这很有意义,但不起作用。Ajax被调用,每次我按下按钮,它都会更新旧的div,而不是执行新的div。@pimvdb是的,我希望在单击后刷新Ajax,并在再次单击时再次执行它。当我们在一个点击事件上停止Ajax时,您不能在同一个按钮上再次点击它。所以问题是每次按下相同的post按钮时如何刷新它。嗨。问题是:在第一次加载页面时,我将一条注释放入$(“#main_text_area”)并按下一个图像按钮来发布文本。Ajax调用$('#posting_main'),设置动画并显示其内部的注释。现在,当我回到文本区域并想放置另一条注释时,Ajax仍然会更新$(“#posting_main”)中已经调用的注释。我想终止这个过程,让Ajax重新启动。当我放置xhr.abort()时;Ajax根本不执行-即使在第一次调用时。。。
     $(document).ready(function() {
    
         var xhr; // by placing it outside the click handler, you don't create
                  // a new xhr each time. Rather, you can access the previous xhr
                  // and overwrite it this way
    
         $("#img_x_ok").click(function(e){
    
             e.preventDefault();
    
             var post_text = $.trim($("#main_text_area").val());
    
             var data_text = 'post_text='+ post_text;
             if (post_text === "") return;
    
             if(xhr) xhr.abort(); // abort current xhr if there is one
    
             xhr = $.ajax({
    
                 type: "POST",
                 url: "comm_main_post.php",
                 data: data_text,
                 cache: false,
    
                 success: function (data){
                     //content
    
                     $("#posting_main").fadeIn();
                     $("#posting_main").load("pull_comm.php");
                     $("#main_text_area").attr("value", "");
    
                     $("#posting_main").animate({ 
                         marginTop: "+=130px",
                     }, 1000 );
    
                 }
    
             });
    
         });
    
    });