简体   繁体   English

Ajax启动事件问题

[英]Ajax start event issue

Im trying to show a progressbar when my page is doing an ajax request. 我试图在页面执行ajax请求时显示进度条。

I have a div with an image inside it and i would like to show it on the ajaxStart event. 我有一个div,里面有一个图像,我想在ajaxStart事件中显示它。 The problem is the img only shows itself when the ajaxStart event is done. 问题是img仅在ajaxStart事件完成时显示。 However the alert is fired before the ajax request. 但是,在ajax请求之前会触发警报。

         $(document).ajaxStart(function () {
            alert("test");

            document.getElementById('LoadingDiv').style.visibility = "visible";

        });
                            $.ajax({
                            url: url,
                            async: true,
                            dataType: 'json',
                            success: function (data) {

Posted abit to early changed async to true and works now. 提前发布有点早,将异步更改为true,现在可以使用。

You can show/hide loading spinner like this: 您可以像这样显示/隐藏加载微调器:

/*LOADING SPINNER*/
jQuery.ajaxSetup({
  beforeSend: function() {
     $('#loadingDiv').show()
  },
  complete: function(){
     $('#loadingDiv').hide()
  },
  success: function() {}
});

The #loadingDiv simply contains an animated image. #loadingDiv仅包含动画图像。 The ajaxSetup method set default values for future Ajax requests. ajaxSetup方法为将来的Ajax请求设置默认值。 That means that this loading indicator will be shown every time when making ajax calls. 这意味着每次进行ajax调用时都会显示此加载指示器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM