在原型中,我可以用下面的代码显示“加载…”图像:
var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars,
onLoading: showLoad, onComplete: showResponse} );
function showLoad () {
...
}
在jQuery中,我可以将服务器页面加载到一个元素中:
$('#message').load('index.php?pg=ajaxFlashcard');
但是我如何附加一个加载旋转到这个命令,因为我在原型?
我也想回答这个问题。我在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
}
});