This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
在下面,您可以看到我正在进行一个简单的ajax调用。但是,在调用成功完成之后,我想从页面中删除loader div。问题是成功函数不触发,为什么?

utils.get_do stuff = function AJAX_do stuff() {
    $.getJSON('/url//',
        function (data) {
            $.each(data, function (key, val) {
                // I do stuff with the data here.
            });
        },
        function (success) {
            $('#loader').hide();
        }
    );
};

最佳答案

这是getJSON的示例,我认为您必须在完整的处理程序中隐藏$('#loader'),因此无论请求失败还是成功,$('#loader')都会隐藏。

 $.getJSON( "/sms/fetch_smartpages/", function() {
    console.log( "success" );
        $.each(data, function (key, val) {
         //I do stuff with the data here.
        });
        $('#loader').hide(); //<==== moved from here
    })
    .done(function() { console.log( "second success" ); })
    .fail(function() { console.log( "error" ); })
    .always(function() {
                console.log( "complete" );
                $('#loader').hide(); // moved here
     });


http://api.jquery.com/jQuery.getJSON/

关于javascript - 进行完整的ajax调用时,无法成功运行jQuery ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16282345/

10-13 02:44