I have a loading div or loader that should show up during Ajax Requests.
我有一个加载div或加载器,应该在Ajax请求期间出现。
$('#loadingDiv').ajaxStart(function () {
$(this).show();
}).ajaxComplete(function () {
$(this).hide();
});
I want the loader to show up during all Ajax Requests, but prevent it on a particular request.
我希望加载器在所有Ajax请求期间显示,但在特定请求中阻止它。
I tried the following:
我尝试了以下方法:
$.ajax({
url: 'Handler.ashx',
success: function (msg, status, xhr) {
$('#loadingDiv').hide();
}.....
But the div shows up then disappears, I don't want it to show up at all.
然后div显示然后消失,我不希望它显示出来。
4 个解决方案
#1
8
Set global
to false when making the request:
在发出请求时将global设置为false:
$.ajax({
url: 'Handler.ashx',
global: false,
...
});
See the Ajax.Events docs.
请参阅Ajax.Events文档。
#2
3
DEMO
Try this
尝试这个
$.ajax({
url: 'Handler.ashx',
beforeSend:function(){
if (<conditon>) /// check your condition here
$('#loadingDiv').show();
else
$('#loadingDiv').hide();
},
success: function (msg, status, xhr) {
$('#loadingDiv').hide();
}
});
Hope this helps,Thank you
希望这有帮助,谢谢
#3
1
Use global: false
in your Ajax options to make the request not trigger any global event handlers.
在Ajax选项中使用global:false可以使请求不会触发任何全局事件处理程序。
The $.ajax
docs say about the global
option:
$ .ajax文档说明了全局选项:
Set to
false
to prevent the global handlers likeajaxStart
orajaxStop
from being triggered.设置为false以防止触发像ajaxStart或ajaxStop这样的全局处理程序。
From the .ajaxStart
docs:
来自.ajaxStart文档:
If
$.ajax()
... is called with theglobal
option set tofalse
, the.ajaxStart()
method will not fire.如果在全局选项设置为false的情况下调用$ .ajax()...,则不会触发.ajaxStart()方法。
#4
0
$.ajax({
url: 'Handler.ashx',
beforesend:function(){
// apply your code
},
success: function (msg, status, xhr) {
$('#loadingDiv').hide();
}.....
reference ajax
参考ajax
#1
8
Set global
to false when making the request:
在发出请求时将global设置为false:
$.ajax({
url: 'Handler.ashx',
global: false,
...
});
See the Ajax.Events docs.
请参阅Ajax.Events文档。
#2
3
DEMO
Try this
尝试这个
$.ajax({
url: 'Handler.ashx',
beforeSend:function(){
if (<conditon>) /// check your condition here
$('#loadingDiv').show();
else
$('#loadingDiv').hide();
},
success: function (msg, status, xhr) {
$('#loadingDiv').hide();
}
});
Hope this helps,Thank you
希望这有帮助,谢谢
#3
1
Use global: false
in your Ajax options to make the request not trigger any global event handlers.
在Ajax选项中使用global:false可以使请求不会触发任何全局事件处理程序。
The $.ajax
docs say about the global
option:
$ .ajax文档说明了全局选项:
Set to
false
to prevent the global handlers likeajaxStart
orajaxStop
from being triggered.设置为false以防止触发像ajaxStart或ajaxStop这样的全局处理程序。
From the .ajaxStart
docs:
来自.ajaxStart文档:
If
$.ajax()
... is called with theglobal
option set tofalse
, the.ajaxStart()
method will not fire.如果在全局选项设置为false的情况下调用$ .ajax()...,则不会触发.ajaxStart()方法。
#4
0
$.ajax({
url: 'Handler.ashx',
beforesend:function(){
// apply your code
},
success: function (msg, status, xhr) {
$('#loadingDiv').hide();
}.....
reference ajax
参考ajax