jQuery / AJAX - 成功时刷新<div> - 不工作

时间:2017-04-25 10:59:54

标签: jquery ajax

我无法(无论出于什么原因)使用从我的AJAX查询中获取的数据填充我的<div id="Last20Requests"></div>标记,即使查询成功并且数据显示在控制台日志中也是如此。我尝试了多种方法来填充包含loadappendprepend等内容,但都无济于事。 我的最后一次尝试是在加载成功的情况下添加一个警告框,但自然它首先不显示。

我不认为我的<div>出了什么问题,但我很感激你对这个谜的帮助。

      <div class="box-header with-border">
        <h3 class="box-title">20 Last Requests</h3><div class="text small pull-right">Refresh</div>
      </div>
      <!-- /. box-header -->
      <div class="box-body">
        <div id="Last20Requests" name="Last20Requests"></div>
      </div>
    </form>
  </div>
  <!-- /.box -->

  <script type="text/javascript" language="javascript">           
    $(function() {
        var prevAjaxReturned = true;
        var xhr = null;

        setInterval(function() {
            if( prevAjaxReturned ) {
                prevAjaxReturned = false;
            } else if( xhr ) {
                xhr.abort( );
            }

            xhr = $.ajax({
                type: 'POST',
                url: '../../plugins/MySQL/ajax_action.php',
                data: {
                    action: 'refresh_rescap_reqs',
                    holidex: '<?php echo($_SESSION['Holidex']); ?>',
                    access: '<?php echo $_SESSION['Access']; ?>'
                },
                dataType: 'json',
                success: function(data) {
                     // data is a string of all output of the server script.
                    //$('#Last20Requests').html(data);
                    //$("#Last20Requests").html(data);
                    //$.find("#Last20Requests").html(data);
                    //$("#Last20Requests").load(JSON.stringify(data));
                    //$("#Last20Requests").load(data);
                    //$("#Last20Requests").append(data);
                    $("#Last20Requests").load(data, function() {
                      alert( "Load was performed." );
                    });
                    prevAjaxReturned = true;
               },
            });

        }, 5000);   // 1000ms = 1 sec
    });
  </script>

1 个答案:

答案 0 :(得分:1)

删除dataType: 'json',因为结果为HTML而不是JSON

xhr = $.ajax({
                type: 'POST',
                url: '../../plugins/MySQL/ajax_action.php',
                data: {
                    action: 'refresh_rescap_reqs',
                    holidex: '<?php echo($_SESSION['Holidex']); ?>',
                    access: '<?php echo $_SESSION['Access']; ?>'
                },
                //dataType: 'json', //remove this line
                success: function(data) {
                     // data is a string of all output of the server script.
                    //$('#Last20Requests').html(data);
                    //$("#Last20Requests").html(data);
                    //$.find("#Last20Requests").html(data);
                    //$("#Last20Requests").load(JSON.stringify(data));
                    //$("#Last20Requests").load(data);
                    //$("#Last20Requests").append(data);
                    $("#Last20Requests").load(data, function() {
                      alert( "Load was performed." );
                    });
                    prevAjaxReturned = true;
               },
            });