jQuery AJAX-是成功还是完成回调? [英] jQuery AJAX - Do Stuff in Success or Complete callbacks?

查看:81
本文介绍了jQuery AJAX-是成功还是完成回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jQuery的AJAX函数有一个快速的疑问.

I have a quick question about jQuery's AJAX function.

我执行ajax调用的方式是按照以下方式进行操作:

The way I do my ajax calls I do something along the lines of:

$.ajax({
    type: "GET",
    url: "/wordpress/wp-admin/admin-ajax.php",
    dataType: 'html',
    data: ({ action: 'loadHomePage'}),
    beforeSend: function() {
        document.getElementById('loading').style.visibility = 'visible';
    },
    complete: function(){
        FB.Canvas.setAutoGrow(false);
        FB.Canvas.setSize({height:600});
        setTimeout(function(){
            FB.Canvas.setAutoGrow(true);
        }, 100);
    },
    success: function(data){
        data = $.trim(data);
        $('#ajax-content').hide().empty().fadeIn('slow').html(data);
        FB.Canvas.scrollTo(0,0);                
    }
});

问题在于,在这种特定情况下,这是一个Facebook iframe内的完整网站,使用ajax进行页面导航,当在页面之间导航时,它似乎多次淡化了内容,因此我想知道最佳实践是什么这里是淡入淡出的部分-应该放在整个部分吗?

The trouble is, in this particular instance, it's a full website inside a facebook iframe that uses ajax for the page navigation, when navigating between pages it seems to fade the content in multiple times so I was wondering what the best practise was here for the fade in part - should it go in the complete part?

我本来就已经完成了调整大小的东西,但是已经移动了它,我认为这有所作为,但是不确定是否是安慰剂效果....

I had the resizing stuff originally in complete but having moved it I think it's made a difference but not sure if it's a placebo effect....

如果是这样,我可以通过执行以下操作来实现它:

If so, do I achieve it by doing the following:

complete: function(data){
    // fade in etc

其次,如果在源代码排序成功之前完成就可以了,还是顺序很重要,例如成功并然后完成?

and secondly, is it ok to do this if complete comes before success in the ordering of the source code or is the order important, e.g. success and then complete?

推荐答案

'complete'触发,并且'success'和'error'(以适合您的请求为准)之后的请求周期结束,并且其定时是独立的在源代码中声明它的位置.

'complete' fires and the end of the request cycle after both 'success' and 'error' (whichever was appropriate for your request) and its timing is independent of where you declare it in the source code.

complete函数不会接收到"data"参数,因此,如果您的操作依赖于数据,那么它将无法在那里工作.

The complete function does not receive the 'data' parameter, so if your action depends on the data then it won't work there.

这篇关于jQuery AJAX-是成功还是完成回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆