为了在执行 ajax 请求时有一个指示器,我得到信息说我应该将带有动画 gif 的指示器放置在页面元素中,然后在 ajax 函数成功时替换数据。
如果我使用 src="ajax-loader.html" 添加指标源,则 ajax 调用会将其保留在原位,并且不会将其替换为数据。如果我使用 .load("ajax-loader.html") 添加指标源,则在 ajax 调用之前它根本不会显示。如果我在 beforesend 事件的 ajax 调用中添加它,它也不会显示。如果我进行两次 ajax 调用,一次加载指标,一次加载数据,同样会发生。必须有一种方法可以在这个简单的代码中显示指标。
这是页面元素的 HTML。
<iframe id="lcupco" style="position: relative; top: 5px; width: 100%; height: 200px; border: 2px solid grey; margin-bottom: 15px;"></iframe>
这是指标的 HTML。
<html>
<head></head>
<body>
<img src='images/ajax-loader.gif'/>
</body>
</html>
这是代码
调用 .load
$(document).ready(function(){ $('#lcupco').load("ajax-loader.html");}); $.ajax({ url: 'luxcal/index.php?cP=40', cache: false, async: true, success: function(data) { $('#lcupco').html(data); }, });
使用前置
`
$.ajax({
url: 'luxcal/index.php?cP=40',
cache: false,
async: true,
beforeSend: function() {
$('#lcupco').load('ajax-loader.html');
// $('#lcupco').html('<html>Initializing calendar...</html>'); //simple text didn't load either.
},
success: function(data) {
$('#lcupco').html(data);
},
});
- 使用两个 ajax 调用:一个用于指标,一个用于数据
`
$.ajax({
url: 'ajax-loader.html',
cache: false,
async: true,
success: function(data) {
$('#lcupco').html(data);
},
});
$.ajax({
url: 'luxcal/index.php?cP=40',
cache: false,
async: true,
beforeSend: function() {
$('#lcupco').html('<html>Initializing calendar...</html>');
},
success: function(data) {
$('#lcupco').html(data);
},
});
`