我如何确定脚本是否正在使用 jQuery 在 Ajax 中加载,例如我正在使用以下 jQuery/AJAX。
$('#select-countries').change(function(){
var countryId = $(this).val();
$.ajax({
type: 'POST',
url: 'process.php',
data: 'countryId='+countryId,
success: function(states){
if(states == 'null') {
$('#select-states-container').html(emptyState);
} else if(states == '0') {
$('#select-states-container').html(chooseState);
} else {
$('#select-states-container').html(states);
}
}
});
});
现在根据加载状态我想显示一条消息或显示一个加载gif,我想知道是否有ajax状态,如加载中,完成时?
最佳答案
我所做的是在ajax调用之前弹出一个加载对话框,然后使用ajax#complete()将其删除或更改消息:
$.ajax({
type: 'POST',
url: 'process.php',
data: 'countryId='+countryId,
beforeSend: function(jqXHR, settings) {
// show the awesome dialog
},
complete: function(jqXHR, textStatus) {
// remove the awesome dialog
},
error(jqXHR, textStatus, errorThrown) {
// post the not so awesome message in the awesome dialog
},
success: function(states){
if(states == 'null') {
$('#select-states-container').html(emptyState);
} else if(states == '0') {
$('#select-states-container').html(chooseState);
} else {
$('#select-states-container').html(states);
}
// change the message in the awesome dialog???
}
});
关于jquery - 我如何确定脚本是否正在使用 jQuery 在 Ajax 中加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6861603/