MAO-EYE 2016-08-07 12:29 采纳率: 100%
浏览 21

AJAX脚本加载-正确顺序

I have a few questions on some behavior I am seeing with my AJAX POST.

I was having issues with using getScript to load 3 .js files in the POST success code. I am seeing on a browser refresh (to test changes) that sometimes the datatable loads (doesn't error out on not a function) and other times it does.

Why does my datatable load work half the time? As I never load datables.js I would expect this to fail every single time. Am I wrong in assuming that javascript loaded in the main page is not available to the AJAX loaded content?

I have tried running async and it doesn't seem to help. I have tried nesting the datatables.js script inside the getScript for bootstrap.js which works, but there is a flicker of the HTML table before the datatable load and it looks really cheesy. When the datatable init is outside the getScript, it doesn't flicker, but as mentioned before, it only works intermittently.

Here is the code:

success: function (result) {
// successful request; do something with the data

$.getScript('js/jquery-2.2.4.js');

$.getScript("js/bootstrap.js", function (data, textStatus, jqxhr) {
    console.log(textStatus); // Success
    console.log(jqxhr.status); // 200
    if (jqxhr.status == 200) {
            //load tooltips (well.. popover)
            $(".itemToolTip").popover({
                trigger: 'hover',
            })

        // required so we can have popups when pagination occurs

            var table = $('#tblItems').DataTable({
                drawCallback: function () {
                    $('[data-toggle="popover"]').popover();
                }
            });
    }
    console.log("Load was performed.");
});

    $('#divTable').html(result);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件