weixin_33686714 2014-04-08 20:20 采纳率: 0%
浏览 19

重新运行Ajax脚本?

If I have a script like below that's ran to load a table with data injected into it from an external PHP file on to the page.

<script>
      $(document).ready(function(){
       var response = '';
        $.ajax({ type: "GET",   
          url: "Records.php",   
             success : function(text)
              {
                response = text;
              }
           });

           alert(response);
         });
</script>

I have another script down below where a user can add records to the database.

<script id="sosurce" language="javascript" type="text/javascript">
$("#newitemadd").click(function(){
  $('#New').modal('show');
    $("#insertResults").click(function(){ 
        var getname = $('#getname').val();
        var getquant = $('#getquant').val();
        var getprice = $('#getprice').val();
        var getdesc = $('#getdesc').val();
               $.ajax({                                      
                    url: 'api2.php',    
                    data: "name="+getname+"&quantity="+getquant+"&price="+getprice+"&description="+getdesc,
                     success: function(data)
                     {  $('#New').modal('hide');
                        $("#success").html(data);
                        $('#success').slideDown().delay(3000).slideUp().reload
                    },
                     error: function() { 

                      $("#failure").alert('show');
                  }       
              }); 
          });
});
</script>

It works fully as intended but, how can I get the first script to Re-Run so the table that's inserted onto the page is refreshed to show the new results that were just added?

  • 写回答

2条回答 默认 最新

  • weixin_33724659 2014-04-08 20:26
    关注

    Move first code into an function like

    <script>
      $(document).ready(LoadData);
    
      function LoadData() {
        var response = '';
        $.ajax({ 
          type: "GET",   
          url: "Records.php",   
          success : function(text) {
            response = text;
          }
        });
    
        alert(response);
      };
    </script>
    

    And call this function from other function, example does it on success

    <script id="sosurce" language="javascript" type="text/javascript">
      $("#newitemadd").click(function() {
        $('#New').modal('show');
        $("#insertResults").click(function() { 
          var getname = $('#getname').val(),
              getquant = $('#getquant').val(),
              getprice = $('#getprice').val(),
              getdesc = $('#getdesc').val();
    
          $.ajax({                                      
            url: 'api2.php',    
            data:       
              "name="+getname+"&quantity="+getquant+"&price="+getprice+"&description="+getdesc,
            success: function(data) {  
              $('#New').modal('hide');
              $("#success").html(data);
              $('#success').slideDown().delay(3000).slideUp().reload
    
              // Call load data again to refresh the table
              LoadData();
            },
            error: function() { 
              $("#failure").alert('show');
            }       
          }); 
        });
      });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥90 请问,这个视频播放软件的名称,用过的朋友请给答案,上方..avi是啥意思?是看短剧还是播放本地视频?
  • ¥15 运筹优化,gurobi,python
  • ¥15 基于python的电影系统推荐
  • ¥20 springmvc重定向和返回json
  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了