【问题标题】:Two Cases of Ajax loading of Webpage having some contradiction网页Ajax加载有一定矛盾的两个案例
【发布时间】:2023-04-01 10:01:01
【问题描述】:

我正在使用 jquery 中的 Ajax 加载函数来加载 DOM 中的另一个页面。 通过使用这个

$('.upload').on('click',function(){

 $('.content').load('loo.php');

});

当我使用这个时,分部内容中的页面会在 3-4 秒间隔后加载。

我想使用该间隔显示进度条,所以我使用了这种方式

$('.upload').on('click',function(){

       $.ajax({
           url: 'loo.php',
         beforeSend:function(){

           res.container.append(res.loader); //Showing loader

         },
         success:function(){

            $('.content').load('loo.php');
             res.container.find(res.loader).remove(); //Hiding Loader 

               }

       });

});

所以现在发生的事情是,加载器出现并显示了几次,然后分页加载,但问题是我再次看到加载器隐藏后页面加载延迟。我创建了加载器来克服时间,但是加载器运行后仍然需要时间。

在 firebug 中,通过分析请求,页面在 loader 之后开始加载,这是我不想要的。 任何想法,如何克服这个问题。

【问题讨论】:

  • 您的脚本正在做的是:附加加载器,发送请求,onSuccess 再次发送请求(通过 load())并隐藏加载器...
  • @ArmelLarcier 知道了,谢谢你告诉我,我只是,需要解决这个问题

标签: javascript jquery ajax


【解决方案1】:

您将在 AJAX 请求完成之前删除“加载程序”。在 load() 完成后,您应该在回调函数中删除加载程序。也不需要双 AJAX 请求。

$('.upload').on('click',function(){
   res.container.append(res.loader); //Showing loader
  $('.content').load('loo.php', function(){
             res.container.find(res.loader).remove(); //Hiding Loader 

    });
});

【讨论】:

    【解决方案2】:

    load()ajax() 都执行 ajax 调用,这可能不是您的本意。试试这个:

    $('.upload').on('click',function() {
        $.ajax({
            url: 'loo.php',
            beforeSend: function() {
                res.container.append(res.loader); //Showing loader
            },
            success: function(data) {
                // add the content to the DOM instead of loading it again
                $('.content').html(data); 
                res.container.find(res.loader).remove(); //Hiding Loader 
            }
        });
    });
    

    【讨论】:

    • 其实我以前用过这个。但是,这给了我数据未定义的错误,指向cloudflare cdn脚本cloudflare.min.js,我认为这是由于云耀斑。这就是为什么我切换到这个,试图再次请求页面,
    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多