jQuery的ajaxStart犯规被触发 [英] Jquery ajaxStart doesnt get triggered

查看:133
本文介绍了jQuery的ajaxStart犯规被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code

$("#loading").ajaxStart(function() {
        alert("start");
        $(this).show();
    });

在我的加价

<div style="text-align:center;"><img id="loading" src="../images/common/loading.gif" alt="" /></div>

下面是完整的Ajax请求:

Here is the full ajax request:

$.ajax({
        type: "POST",       

        url: "http://localhost/WebServices/Service.asmx/GetResults",

        data: jsonText,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {

            var results = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            PopulateTree(results);
        },

        error: function(xhr, status, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.Message);


        }
    });

$("#loading").ajaxStart(function() {
        alert("start");
        $(this).show();
    });

    $("#loading").ajaxStop(function() {
        alert("stop");
        $(this).hide();
        $("#st-tree-container").show();

    });

永远不会触发警报开始,即使GIF显示为旋转。 AjaxStop被触发预期。 任何想法,为什么?

never fires alert "start" even though the gif is shown to rotate. AjaxStop gets triggered as expected. Any ideas why?

推荐答案

它没有得到触发,因为你的处理程序 .ajaxStart() 未注册,直到 Ajax请求已经去(过去时,它会被调用)。该 .ajaxStop() 是经过注册为好,但<强>在的要求完成,所以当它回来它挂接运行。

It's not getting triggered because your handler for .ajaxStart() isn't registered until after the ajax request is already going (past when it would have been called). The .ajaxStop() is registered after as well, but before the request finishes, so when it comes back it is hooked up to run.

要解决这个问题,将这个之前您首先 $。阿贾克斯( ) 电话:

To fix this, move this before your first $.ajax() call:

$("#loading").ajaxStart(function() {
  $(this).show();
}).ajaxStop(function() {
  $(this).hide();
  $("#st-tree-container").show();
});


更新:启动jQuery的1.9,AJAX事件应附只有文件 <一href="http://jquery.com/upgrade-guide/1.9/#ajax-events-should-be-attached-to-document">http://jquery.com/upgrade-guide/1.9/#ajax-events-should-be-attached-to-document


UPDATE: Starting jQuery 1.9, AJAX events should be attached to document only. http://jquery.com/upgrade-guide/1.9/#ajax-events-should-be-attached-to-document

$(document).ajaxStart(function() {
  $("#loading").show();
});

$(document).ajaxStop(function() {
  $("#loading").hide();
  $("#st-tree-container").show();
});

这篇关于jQuery的ajaxStart犯规被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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