親 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;
};