0

我的文件准备好了:

$(document).ready(function () {

     var apiKey = "58e278fc-9d57-49bb-be96-9e85b847d5b5";

     $("a#showA").click(function (e) {
         e.preventDefault();
         getHotelInfo(apiKey);
     });
});

我调用 JSON API 来获取合约名称(其中 2 个)

function getHotelInfo(yourAPIKey) {
var enquiry = "http://api.roomex.com/api/hotel?apiKey=" + yourAPIKey;
//alert(enquiry);
 $.ajax({
 url: enquiry,
 type: 'GET',
 dataType: "jsonp",
 jsonp: "callback",
 jsonpCallback: "jsonpCallback2",
 complete: function (response, responseCode) {
 },
 success: function (json) {
       $.each(json.Contracts, function (index, contract) {
         getAvailability(yourAPIKey, contract.ContractCode, startDate, endDate);
         getRates(yourAPIKey, contract.ContractCode, startDate, endDate);
         //alert(contract.ContractCode + " - done ");
       });
     }
 });
 }

两个函数:getAvailability 和 getRates - 应该运行两次。它几乎没有延迟(如果我发出那个警报)当我删除那个警报时问题就发生了。

这是这两个功能和第三个 - 填充结果/

function populateValues(Type, Contractname, RoomType, Date, val) {
$("input#" + Type + "_IE-ORK-IN-32966275_" + Contractname + "_" + RoomType + "_" + Date).val(val);
// alert("input#" + Type + "_" + Contractname + "_" + RoomType + "_" + Date + " -> " + val);
}

function getRates(yourAPIKey, contractCode, startDate, endDate) {
 var ratesEnquiry = "http://api.roomex.com/api/rate?apiKey=" + yourAPIKey + "&contractCode=" + contractCode + "&startDate=" + startDate + "&endDate=" + endDate;

 $.ajax({   
     url: ratesEnquiry,
     type: 'GET',
     dataType: "jsonp",
     jsonp: "callback",
     jsonpCallback: "jsonpCallback3",
     complete: function (response, responseCode) {
         //console.log(response); console.log(responseCode);
         //alert("complete");           
     },
     success: function (json) {
         $.each(json, function (index, value) {
             populateValues("rate", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.RoomPrice);
             populateValues("hrate", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.RoomPrice);                 
         });
     }, 
 });
}



function getAvailability(yourAPIKey, contractCode, startDate, endDate) {
var availabilityEnquiry = "http://api.roomex.com/api/availability?apiKey=" + yourAPIKey + "&contractCode=" + contractCode + "&startDate=" + startDate + "&endDate=" + endDate;
$.ajax({
    url: availabilityEnquiry,
    type: 'GET',
    dataType: "jsonp",
    jsonp: "callback",
    jsonpCallback: "jsonpCallback",
    complete: function (json, responseCode) {
        //console.log(response); console.log(responseCode);
        //alert("complete");           
    },
    success: function (json) {
        $.each(json, function (index, value) {
            //populateAvailability(this.Date.substr(0, 10), this.RoomTypeCode, this.ContractCode, this.Quantity);
            populateValues("avail", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.Quantity);
            populateValues("havail", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.Quantity);              
        });
    }, async: false
});
}

var startDate = "2012-07-24";
var endDate = "2012-07-31";

出现问题是因为 od 异步。但我不知道如何解决这个问题。你们能帮帮我吗?

4

1 回答 1

1

等待第一个异步请求完成,然后执行第二个,然后显示结果。

例如:

async_func1(async_func2(function(result){
//show results
}));

其中 async_func1 firsts 参数是请求完成时的回调。

于 2012-07-27T13:33:57.147 回答