显示使用jQuery,同时使一个AJAX请求加载条 [英] Show a loading bar using jQuery while making a AJAX request

查看:79
本文介绍了显示使用jQuery,同时使一个AJAX请求加载条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Ajax请求用jQuery这样的:

I'm making a AJAX request with jquery like:

$.get('/Stuff.php', function (data) {
    $('#own').html(data);
});

而这个数据是加载我想显示在页面的顶部有一个小的文本(这只是说,载入中...)的没有阻止它

如何做到这一点?

推荐答案

3nigma是在正确的轨道,但有一个细节错了,至少在一般情况下。

3nigma is on the right track, but got one detail wrong, at least in the general case.

使用ajaxSetup只提供默认值,如果以后你作出这样的指定自己的回调beforeSend(即你需要设置一些特定的头文件)或完整(要同时处理成功和错误的一样),他们将覆盖一些Ajax调用那些在ajaxSetup,你的负荷指标将打破。

Using ajaxSetup only provides defaults, if later you make some ajax calls that specify their own callbacks for beforeSend (i.e. you need to set some specific headers) or complete (you want to handle both success and error the same) they will override the ones in ajaxSetup and your loading indicator will break.

相反,使用全局AJAX事件(的more有关Ajax事件的)

$(document).ajaxSend(function(e, jqXHR){
  //show the loading div here
});
$(document).ajaxComplete(function(e, jqXHR){
  //remove the div here
});

这是不会打破,即使其他code也希望这样设置一些全局或局部beforeSend /完整的处理程序或调用ajaxSetup一个更通用的解决方案。

This is a more generic solution that will not break even if other code also wants so set some global or local beforeSend/complete handlers or calls ajaxSetup.

这篇关于显示使用jQuery,同时使一个AJAX请求加载条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆