这段代码
$("#loading").ajaxStart(function() {
alert("start");
$(this).show();
});
Run Code Online (Sandbox Code Playgroud)
在我的加价
<div style="text-align:center;"><img id="loading" src="../images/common/loading.gif" alt="" /></div>
Run Code Online (Sandbox Code Playgroud)
这是完整的ajax请求:
$.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();
});
Run Code Online (Sandbox Code Playgroud)
即使gif显示为旋转,也不会触发警报"开始".AjaxStop按预期触发.有什么想法吗?
Nic*_*ver 47
它没有被触发,因为你的处理程序.ajaxStart()直到ajax请求已经开始之后才被注册(过去它将被调用).在.ajaxStop()被注册后为好,但之前要求完成,所以当它回来了它挂接运行.
要解决此问题,请在第一次$.ajax()通话前移动:
$("#loading").ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
$("#st-tree-container").show();
});
Run Code Online (Sandbox Code Playgroud)
更新:启动jQuery 1.9,AJAX事件应该只附加到文档.
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();
});
Run Code Online (Sandbox Code Playgroud)