【问题标题】:AJAX call prevents navigating to other pages until it has finishedAJAX 调用阻止导航到其他页面,直到它完成
【发布时间】:2020-04-12 21:54:23
【问题描述】:

这是我的 HTML5 仪表板页面的流程,它异步显示多个 Google 图表。

  1. 为每个图表进行异步 AJAX 调用以检索数据。
  2. AJAX 调用调用 PHP 文件。
  3. PHP 文件从 Oracle 数据库中检索数据。
  4. 在检索每个图表的数据时,会呈现相应的图表。

这是一个非常酷的仪表板 :-)

问题来了:当我单击同一页面上的另一个超链接(例如指向“设置”页面的链接)时,浏览器的进度图标会旋转,并且在所有图表加载完成之前它不会导航到新页面。

我认为进行“异步”调用可以让您离开页面。即使所有图表都没有完成加载,我也想离开图表页面。

我使用 Microsoft IIS 作为 Web 服务器和 PHP 7.4。

这是我进行 AJAX 调用的代码。

function drawChart(chartType, chartDivName, dataFileName, dataFileParam1, dataFileParam2) {
    var chartDiv = document.getElementById(chartDivName);

    // first, display the loading message
    chartDiv.innerHTML = "<img class='img-fluid' src='/images/loading.gif' alt='Loading...'>";

    // get the data asynchronously
    $.ajax({
        url         : dataFileName,
        method      : "POST",
        data        : {param1: dataFileParam1, param2: dataFileParam2},
        dataType    : "JSON",
        async       : true,
        success     : function(data) {
            displayChart (chartDiv, data);
        },
        error       : function(xhr, status, error) {
               chartDiv.innerHTML = "show the error";
        }
    });

} // end drawChart

这就是我调用上述函数的方式:

<!-- Load charts -->
<script>
    // set the reportDate variable and the report-date form field
    reportDate = getYesterdayDate();
    $('#report-date').val(reportDate);

    /*
    **
    ** Place all charts and dynamic data calls that need to be loaded on the pgae in this function.
    ** This function should include the call to loading the daily charts.
    **
    */
    function loadAllCharts() {
        //load daily charts and dynamic data
        loadDailyCharts();

        // load non-daily charts and dynamic data
        getHtmlData("mtd-order-total", '/om/data/mtd_om_total.php', '', '');
        getHtmlData("mtd-ar-total", '/ar/data/mtd_ar_total.php', '', '');
        drawChart('column', 'monthly-orders-chart', '/om/chart-data/monthly_om_totals.php', '', '');
        drawChart('column', 'monthly-ar-chart', '/ar/chart-data/monthly_ar_totals.php', '', '');
    }

    /*
    **
    ** Place all charts and dynamic data calls that need to be refreshed when the date field is changed
    ** in this function.
    **
    */
    function loadDailyCharts() {
        //load dynamic data
        getHtmlData("yday-order-total", '/om/data/daily_om_total.php', reportDate, '');
        getHtmlData("yday-ar-total", '/ar/data/daily_ar_total.php', reportDate, '');

        // load charts
        drawChart('single-column', 'day-orders-chart', '/om/chart-data/daily_om_by_source.php', reportDate, '')
        drawChart('single-column', 'day-ar-chart', '/ar/chart-data/daily_ar_by_type.php', reportDate, '');
    }


    // Load all charts on page load
    google.charts.setOnLoadCallback(loadAllCharts);


    // Reload charts when report-date is changed
    $('#refresh-report').click(function() {
        reportDate = $("#report-date").val();
        loadDailyCharts();
    });

</script>

【问题讨论】:

    标签: javascript php jquery html ajax


    【解决方案1】:

    您的 .ajax 调用是通过 $(document).ready jquery 函数进行的吗?在该上下文中进行的 AJAX 调用可能会阻止在初始 Javascript 执行后触发的 window.load() 事件。尝试使用 window.load() 而不是 document.ready()。

    仅供参考——这只是我的最佳猜测。如果您可以与我分享您的 html 以及围绕您的函数调用的 Javascript,我可以做出更好的猜测。

    【讨论】:

    • 嗨,Jibreel,我用加载 Google 图表的 HTML 中的完整 javascript 修改了我的帖子。我使用“google.charts.setOnLoadCallback(loadAllCharts);”加载图表。你能检查我的代码和评论吗?
    • 好吧,我的下一个猜测是看“getHTMLData()”。那些功能是什么?他们是 GET 吗?它们是同步 GET 吗?如果您的 getHTML 函数是同步运行的,那么这意味着您的页面将最终“等待”每个图表加载,除了最后两个在最后一次 getHTML 调用之后出现的图表。 drawChart('column', 'monthly-orders-chart', '/om/chart-data/monthly_om_totals.php', '', ''); drawChart('column', 'monthly-ar-chart', '/ar/chart-data/monthly_ar_totals.php', '', '');您的整个网站是否都依赖于这些 getHtml 函数?
    • 如果你把它们注释掉,你的代码是否仍然同步运行?知道这将有助于缩小范围。
    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多