【问题标题】:How do I return the result of an AJAX call from jQuery.steps?如何从 jQuery.steps 返回 AJAX 调用的结果?
【发布时间】:2018-04-25 21:12:03
【问题描述】:

我知道一个非常相似的问题是asked and answered here,但那里的答案对我不起作用。我不知道我是否做错了什么,或者过去 18 个月中 jQuery 或 jQuery.steps 是否发生了变化。

onStepChanging 方法中,我有以下代码(为了清楚起见,省略了检查我们所处的步骤等的代码)...

    $.ajax({
      url: '/Home/ValidatePostcode',
      type: "GET",
      data: {
        postcode: postcode
      },
      success: function (result) {
        if (result) {
          return true;
        } else {
          return false;
        }
      }
    });

但是,这不起作用。 onStepChanging 方法在 AJAX 调用之前返回,因此 success 回调中的 return 语句被忽略。根据链接帖子中的答案,这应该可行。

不是我想同步执行此操作,而是尝试将 async: false 添加到 AJAX 调用中,但没有任何区别。

有人知道我怎样才能让它工作吗?我用的是jQuery.steps v1.0.1(今天下午下载的,所以他最新版)和jQuery v1.12.3。

【问题讨论】:

    标签: jquery jquery-steps


    【解决方案1】:

    我认为你很接近。

    关键是您尝试过的“async: false”,但是您的“return true;”和“返回错误;”从 ajax 成功方法内部只返回到 ajax 调用。 真/假值与调用堆栈上的原始 onStepChanging 方法无关。

    • 在 ajax 调用之前创建一个变量,比如“var response = false;”
    • 然后将 async: false 添加到 ajax 调用中
    • 然后在您的成功方法中设置 response = true;
    • 然后在onStepChanging 方法中返回响应变量。 “返回响应;”

    所有的组合可能看起来像这样:

    onStepChanging: function (...)
    {
        var response = false;
    
        $.ajax({
            url: '/Home/ValidatePostcode',
            type: "GET",
            async: false,
            data: {
                postcode: postcode
            },
            success: function (result) {
                if (result) {
                    response = true;
                }
            }
        });
    
        return response;
    }
    

    应该可以的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2018-01-09
      • 2023-04-01
      相关资源
      最近更新 更多