等待Ajax响应相同功能 [英] Waiting for ajax response same function

查看:220
本文介绍了等待Ajax响应相同功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,类似的问题已经被贴了很多次,但我读过许多人并不能找到一个答案,我的问题。

I know that similar questions have been posted many times, however I've read many of them and can't find an answer to my problem.

我有一个函数,等待Ajax请求的响应。你们中许多人会问,为什么?好吧,我使用的是向导jQuery插件,它执行的函数 onLeaveAStepFunction 时的一个步骤是左,则向导进入所选择的步骤,如果从返回值 onLeaveAStepFunction 是真实的;否则它仍然在同一个步骤

I have a function that waits for an ajax request response. Many of you will ask why? Well, I'm using a Wizard Jquery Plugin which executes a function onLeaveAStepFunction when a step is left, then the wizard goes to the selected step if the return value from onLeaveAStepFunction is true; else it remains in the same step.

我这样做的 异步:假 的等待和它的作品,但是这是一个糟糕的设计。另外,我无法使用blockUI插件。

I'm doing this async: false for waiting and it works, but this is a bad design. Also, I can't use a blockUI plugin.

我怎样才能做到这一点?

How can I do this?

有些code:

初​​始化向导:

$("#wizard").smartWizard({
        onLeaveStep : onLeaveStepFunction,
    });

调用Ajax请求:

Calling the ajax request:

function onLeaveStepCallback(obj, context) {
    nextStep = sendForm();
}

Ajax请求:

The ajax request:

var nextStep = false;
$.ajax({
    url : path,
    type : "POST",
    async : false,
    data : $("#" + idForm).serialize(),
    success : function(data) {
        $("#" + idDiv).html(data);
        nextStep = !$("#" + idHiddenErrores).val())
    }
});

忽略的属性。请帮我。

Omitting the attributes. Please help me.

推荐答案

一hackish的解决办法是leaveStep之前做到这一点。也许在showStep:

One hackish work-around is to do it before leaveStep. Perhaps on showStep:

var wizard_next_step;
$("#wizard").smartWizard({
    onShowStep : function (obj, context) {
        onLeaveStepFunction(obj, context, function(nextStep){
            wizard_next_step = nextStep;
        });
    },
    onLeaveStep : function () {
        return wizard_next_step;
    }
});

您会还需要修改 onLeaveStepFunction 来接受一个回调:

You'd also need to modify your onLeaveStepFunction to accept a callback:

function onLeaveStepCallback(obj, context, callback) {
    nextStep = sendForm(callback);
}

和你的AJAX功能,然后应该是:

And your ajax function should then be:

$.ajax({
    url : path,
    type : "POST",
    async : false,
    data : $("#" + idForm).serialize(),
    success : function(data) {
        $("#" + idDiv).html(data);
        callback( !$("#" + idHiddenErrores).val()) );
    }
});

现在,它看起来像你正在绘制到向导窗口,这样的:

Now, it looks like you're drawing into the wizard window with this:

$("#" + idDiv).html(data);

我完全确信,如果是这种情况。但如果是,那么你不能这样做在这里(很明显,因为它是 onShowStep 这将改写当前的内容)。如果是这样的话,你应该传递数据的回调:

I'm entirely sure if this is the case. But if it is then you cannot do this here (obviously because it's onShowStep which would overwrite current content). If this is so you should pass the data in the callback:

success : function(data) {
    callback( data , !$("#" + idHiddenErrores).val()) );
}

这样写的精灵:

Write the wizard like this:

var wizard_next_step;
var wizard_data;
$("#wizard").smartWizard({
    onShowStep : function (obj, context) {
        onLeaveStepFunction(obj, context, function(data, nextStep){
            wizard_data = data;
            wizard_next_step = nextStep;
        });
    },
    onLeaveStep : function (obj, context) {
        $("#" + idDiv).html(wizard_data);
        return wizard_next_step;
    }
});

关键是要呼吁所有的异步函数和长期获取数据,你打电话给你的所有的同步功能了。

The key is to call all the asynchronous functions and get the data long before you call all your synchronous functions.

注:我不知道的智能向导,在所有的,而不是一个严重的jQuery的用户。上面的答案是基于我的2分钟阅读GitHub上的智能向导,文档和我的JavaScript的理解。你一定会需要修改我的例子,使其工作。

Note: I don't know smart-wizard at all and not a serious jQuery user. The answer above is based on my 2 minutes reading smart-wizard documentation on github and my understanding of javascript. You will definitely need to modify my examples to make it work.

这篇关于等待Ajax响应相同功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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