Jquery ajax与父级ajax.done

时间:2013-03-15 23:18:35

标签: jquery

如果嵌套在父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;
    };

1 个答案:

答案 0 :(得分:0)

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

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

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