简体   繁体   English

等待使用yii2表单的ajax响应

[英]Wait for ajax response using a yii2 form

I am implementing a form that request a process with ajax to sync 2 databases and I need to wait until the process finish. 我正在实现一个表单,该表单请求使用ajax的进程同步2个数据库,因此我需要等到该进程完成。

In my form I have the flowing code to send the db credentials: 在我的表单中,我有下面的代码发送数据库凭证:

<div class="col-lg-offset-1 col-lg-11">
<?=
Html::a('Sync Databases', ['runsync'], [
    'class' => 'btn btn-primary',
    'id' => 'ajax_link_02',
    'data-on-done' => 'linkFormDone',
    'data-form-id' => 'syncdb_form',]
)
?>        

and I also register the js with the ajax 而且我还用ajax注册了js

function handleAjaxLink(e) {
    e.preventDefault();
    var
        $link = $(e.target),
        callUrl = $link.attr('href'),
        formId = $link.data('formId'),
        onDone = $link.data('onDone'),
        onFail = $link.data('onFail'),
        onAlways = $link.data('onAlways'),
        ajaxRequest;

        $("#show_msg").html("Loading.... please wait...");

        ajaxRequest = $.ajax({
        type: "post",
        dataType: 'json',
        url: callUrl,
        data: (typeof formId === "string" ? $('#' + formId).serializeArray() : null)
    });

    // Assign done handler
    if (typeof onDone === "string" && ajaxCallbacks.hasOwnProperty(onDone)) {
        ajaxRequest.done(ajaxCallbacks[onDone]);
    }

    // Assign fail handler
    if (typeof onFail === "string" && ajaxCallbacks.hasOwnProperty(onFail)) {
        ajaxRequest.fail(ajaxCallbacks[onFail]);
    }

    // Assign always handler
    if (typeof onAlways === "string" && ajaxCallbacks.hasOwnProperty(onAlways)) {
            ajaxRequest.always(ajaxCallbacks[onAlways]);
        }

    }

var ajaxCallbacks = {
        'linkFormDone': function (response) {
            $('#show_msg').html(response.body);
        }

    }

The ajax call works fine and call the controller: ajax调用工作正常并调用控制器:

public function actionRunsync() {
    if (Yii::$app->request->isAjax) {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = new \app\modules\admin\models\syncdb();
        $response = $model->runSyncDb($_POST);
        $res = array(
            'body' => $response,
            'success' => true,
        );

        return $res;
    }
}

But is not waiting to the process 但不是在等待过程

$model->runSyncDb($_POST)

to finish. 完成。 If I comment the process and add a text it works fine but not if the process takes long. 如果我对过程进行注释并添加文本,则可以正常工作,但是如果过程花费很长时间,则无法正常工作。

What am I doing wrong? 我究竟做错了什么?

I just added async false to the ajax request: 我只是在ajax请求中添加了async false:

                async: false,

so now the ajax is like this: 所以现在的ajax是这样的:

    ajaxRequest = $.ajax({
      type: "post",
      async: false,
      dataType: 'json',
      url: callUrl,
      data: (typeof formId === "string" ? $('#' + formId).serializeArray() : null)
    });

Not sure the best way but works perfectly... 不确定最好的方法,但是效果很好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM