简体   繁体   English

带有MVC的jQuery AJAX顺序调用

[英]jQuery AJAX sequential calls with MVC

I've got an odd setup in an MVC project, where I have an @Ajax.BeginForm() with the following fields:- 我在MVC项目中有一个奇怪的设置,在这里我有一个带有以下字段的@Ajax.BeginForm()

  • Address Line 1 地址第一行
  • Address Line 2 地址行2
  • Town
  • County
  • PostCode 邮编

We require to go to Google Geocode with this address, and get the latitude and longitude on click of the submit button on the Ajax form. 我们需要使用此地址转到Google Geocode,并在Ajax表单上单击“提交”按钮以获取经度和纬度。 So:- 所以:-

@using (Ajax.BeginForm("EditLocation", "Location", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "new-locations", OnBegin = "return OnBegin()", OnSuccess = "OnSuccess()", InsertionMode = InsertionMode.InsertBefore }, new { @id = "EditLocationForm" })) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)
    <!-- Standard Text Fields -->
    @Html.TextBoxFor(m => m.Latitude)
    @Html.TextBoxFor(m => m.Longitude)
    <input type="submit" class="btn btn--gradient btn--right" id="EditLocationSubmit" />  
}

function OnBegin() {
    $.ajax({
        url: '/Location/CheckAddress/',
        datatype: "json",
        async: false,
        data: { addressLine1: $("#BrandLocationAddress_AddressLine1").val(), addressLine2: $("#BrandLocationAddress_AddressLine2").val(), town: $("#BrandLocationAddress_Town").val(), county: $("#BrandLocationAddress_County").val(), postcode: $("#BrandLocationAddress_Postcode").val(), },
        type: "POST",
        success: function (data) {
            if (data !== "NORESULTS") {
                $("#Latitude").val(data.Latitude);
                $("#Longitude").val(data.Longitude);
                return true;
            }
            else {

            }
        },
        error: function (response) {
            console.log(response);
            return false;
        }
    });
}

The results of the /Location/CheckAddress/ bring back a latitude and longitude as expected, but the Ajax.BeginForm() goes into the form submission before the two #Latitude and #Longitude fields are populated. / Location / CheckAddress /的结果返回了预期的纬度和经度,但是在填充两个#Latitude和#Longitude字段之前, Ajax.BeginForm()进入了表单提交。 When I look in the [HttpPost] of the model, latitude and longitude are 0. 当我查看模型的[HttpPost]时,纬度和经度为0。

When I post a second time, the fields are the correct latitude and longitude values. 当我第二次发布时,这些字段是正确的纬度和经度值。 I think this might be because OnBegin() is allowing the Ajax.BeginForm to do its stuff before the last line of the success clause has been executed, hence 0. 我认为这可能是因为OnBegin()允许Ajax.BeginFormsuccess子句的最后一行执行之前完成其工作,因此为0。

Can anyone advise on a way to make the second form delay submission until I know the last line of the success clause has been completed? 在我知道success条款的最后一行完成之前,有人可以建议第二种形式的延迟提交吗?

You need to return false from your function: 您需要从函数中返回false

var allowSubmit = false;
function OnBegin() {
    if (allowSubmit == true) { return true; }
    $.ajax({
        // ... code
        success: function (data) {
            // ... code
            // here you need to submit the form using 
            $( "#EditLocationForm" ).submit()
            allowSubmit = true;
        },
        error: function (response) {
          // .. code
        }
    });

    return false;
}

In success , submit the form as shown in code above. success ,提交上面的代码中所示的表单。

Also, do not do this using async: false : you do not want to freeze the UI while that operation is going on. 另外,不要使用async: false执行此操作async: false :在执行该操作时,您不想冻结UI。 What you may want to do is to disable the controls so the user cannot change them and then re-enable them in success and error . 您可能想要做的是禁用控件,以便用户无法更改它们,然后在successerror重新启用它们。

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

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