jquery - 我如何确定脚本是否正在使用 jQuery 在 Ajax 中加载?

标签 jquery ajax

我如何确定脚本是否正在使用 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/

相关文章:

javascript - Bootstrap Datetimepicker 以编程方式从 Epoch 设置时间

JavaScript - 预加载图像

javascript - 如何使用 AJAX 将 DropDown 保存到数据库中

php - 为什么我的 jQuery AJAX 调用会在浏览器中打开响应?

javascript - 正确的异步请求处理 - JS

javascript - 如何将 jQuery 与 React js 集成?

php - Symfony2 在每个循环中更改元素样式

jquery - 如果 javascript 关闭,则可以显示/隐藏

javascript - 单击“应用”按钮并插入到另一个表中时从面板获取名称

javascript - AJAX get 请求将每个字母放入数组中