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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)