在原型中,我可以用下面的代码显示“加载…”图像:

var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, 
onLoading: showLoad, onComplete: showResponse} );

function showLoad () {
    ...
}

在jQuery中,我可以将服务器页面加载到一个元素中:

$('#message').load('index.php?pg=ajaxFlashcard');

但是我如何附加一个加载旋转到这个命令,因为我在原型?


当前回答

我使用jQuery UI对话框。(也许它也适用于其他ajax回调?)

$('<div><img src="/i/loading.gif" id="loading" /></div>').load('/ajax.html').dialog({
    height: 300,
    width: 600,
    title: 'Wait for it...'
});

包含一个动画加载gif,直到ajax调用完成时其内容被替换为止。

其他回答

我也想回答这个问题。我在jQuery中寻找类似的东西,这是我最终使用的。

我从http://ajaxload.info/得到了我的加载转轮。我的解决方案基于http://christierney.com/2011/03/23/global-ajax-loading-spinners/上的这个简单答案。

基本上你的HTML标记和CSS看起来是这样的:

<style>
     #ajaxSpinnerImage {
          display: none;
     }
</style>

<div id="ajaxSpinnerContainer">
     <img src="~/Content/ajax-loader.gif" id="ajaxSpinnerImage" title="working..." />
</div>

然后你为jQuery编写的代码看起来像这样:

<script>
     $(document).ready(function () {
          $(document)
          .ajaxStart(function () {
               $("#ajaxSpinnerImage").show();
          })
          .ajaxStop(function () {
               $("#ajaxSpinnerImage").hide();
          });

          var owmAPI = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=YourAppID";
          $.getJSON(owmAPI)
          .done(function (data) {
               alert(data.coord.lon);
          })
          .fail(function () {
               alert('error');
          });
     });
</script>

就是这么简单:)

请注意,旋转器必须使用异步调用才能工作(至少这是导致我的旋转器直到ajax调用之后才显示的原因,然后在调用结束并删除旋转器时迅速消失)。

$.ajax({
        url: requestUrl,
        data: data,
        dataType: 'JSON',
        processData: false,
        type: requestMethod,
        async: true,                         <<<<<<------ set async to true
        accepts: 'application/json',
        contentType: 'application/json',
        success: function (restResponse) {
            // something here
        },
        error: function (restResponse) {
            // something here                
        }
    });

你总是可以使用Block UI jQuery插件,它为你做的一切,它甚至在ajax加载时阻塞任何输入的页面。如果这个插件似乎不能正常工作,你可以在这个回答中阅读正确的使用方法。来看看。

这是一个非常简单和智能的插件,用于特定的目的: https://github.com/hekigan/is-loading

变体:我在主页的左上角有一个id="logo"的图标;当ajax工作时,一个旋转GIF被覆盖在顶部(透明)。

jQuery.ajaxSetup({
  beforeSend: function() {
     $('#logo').css('background', 'url(images/ajax-loader.gif) no-repeat')
  },
  complete: function(){
     $('#logo').css('background', 'none')
  },
  success: function() {}
});