等待Ajax响应相同功能功能、Ajax

2023-09-10 19:46:47 作者:待我前途伟大炸飞清华北大

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

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.

 
精彩推荐
图片推荐