0

如果嵌套在父 ajax 请求的 .done() 方法中,是否存在阻止 jquery ajax 调用正确执行的问题?嵌套 ajax 调用的 .done() 似乎没有被执行。非常感谢您提供的任何帮助。

var findNearestFacility = function () {
    var $form = $(this);
    var $addrsInput = $("form[data-ucw-ajaxNearestFacility] input.nearest-facility-input").attr('value')

    var geocoderInput = new google.maps.Geocoder();


    ////// calculate longitude and latitude for address entered in
    geocoderInput.geocode({ 'address': $addrsInput }, function (results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            latitudeUser = results[0].geometry.location.lat();
            longitudeUser = results[0].geometry.location.lng();

            var facilities = [];

            var facilitiesAndDistance = new Array();

            var options = {
                dataType: 'json',
                type: 'GET',
                url: '/Facilities/AllFacilities'
            };

            $.ajax(options).done(function (data1) {

                $.each(data1, function () {
                    this.ConcatAddress = this.StreetAddress1 + ", " + this.City + ", " + this.State + ", " + this.Zip;
                    facilities.push(this);
                });

                maxLengthFacilities = facilities.length;  // store max length of array

                var length = facilities.length;

                for (var indexFacility = 0; indexFacility < length; indexFacility++) {

                    var currentAddress = facilities[indexFacility].ConcatAddress;

                    geoCode(facilities[indexFacility], indexFacility, latitudeUser, longitudeUser);

                }

                var i = facilities;

                var facilitiesJson = $.toJSON(facilities);

                var options2 = {
                    url: "/Home/NearestFacilities",
                    type: 'POST',
                    dataType: 'json',
                    data: facilitiesJson
                };

                $.ajax(options2).done(function (returnData) {
                    // HELP!!
                    // CODE IN THIS BLOCK NEVER EXECUTES

                    alert('x');

                    //var $target = $($form.attr('data-ucw-target'));
                    //var $newHtml = $(data2);
                    //$target.replaceWith($newHtml);
                    //$target.effect('highlight');

                    //alert(data2);
                });

            })
        };
    });


    return false;
}

更新:感谢您建议简化代码,因为它揭示了。下面是简化版。该测试按预期工作,因此问题变成了原始代码有什么问题。

var nestedAjexTest = function () {
        var options = {
            dataType: 'json',
            type: 'GET',
            url: '/Facilities/Ajax1'
        };

        $.ajax(options).done(function (data1) {
            alert("done1 " + data1);

            var options2 = {
                url: "/Facilities/Ajax2",
                type: 'POST',
                dataType: 'json',
                data: data1
            };

            $.ajax(options2).done(function (data2) {
                alert("done2 " + data2);
            })


        })

        return false;
    };
4

1 回答 1

0

我花了一些时间研究这种情况,但我无法复制或诊断问题。请参阅以下使用延迟方法的嵌套 ajax 请求的工作示例。

$.ajax().done(function(){
   $.ajax().done(function() {
       console.warn('success');
   });
});

我建议删除您的代码片段,直到您的嵌套done()正确触发。地理编码器有可能会以某种方式干扰,但在等待 javascript 错误时,我不知道如何。

于 2013-03-16T00:17:17.280 回答