JSON中的异步。函数只运行一次而不是两次

时间:2012-07-27 11:42:12

标签: jquery jsonp

我的文件。已经:

$(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异步而出现问题。但我不知道如何解决这个问题。你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

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

E.g:

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

async_func1第一个参数是请求完成时的回调。