一起添加两个单独的ajax Post请求结果

时间:2012-10-01 10:13:25

标签: jquery ajax

我有两个单独的Ajax Post请求需要一起添加,它们都链接到货币交换源,它基本上是两个从不同货币转换的值,然后需要在转换为一个后加在一起货币,我已经用同一种货币获得它们我不需要将两个结果加在一起以获得最终总数。

以下是两个请求

    var dataString = "amount=" + entireTotal + "&from=" + from + "&to=" + to;
        //Lets Get the exchange rates from our total
     $.ajax({
       type: "POST",
       url: "http://fileserver/website/modules/mod_calculation/js/currency.php",
       data: dataString,
       success: function(data){
        $('#inputresult').show();
        //Put received response into result div
         $('#inputresult').html(data);
       }
     });

var dataString = "amount=" + amountGel + "&from=" + fromGel + "&to=" + toGel;
            $.ajax({
       type: "POST",
       url: "http://fileserver/website/modules/mod_calculation/js/currencygel.php",
       data:  dataString,
       success: function(data){
           $('#resultsgel').html(data);
         //Show results div
         $('#resultsgel').show();
        //Put received response into result div
       }
     });

任何帮助都会有很大帮助..谢谢:)。

1 个答案:

答案 0 :(得分:3)

var request1 = $.ajax({ ... }),
    request2 = $.ajax({ ... });

$.when(request1, request2).then(function(response1, response2) { ... })

进一步阅读:

类似于你需要的小抽象示例(抱歉,没有足够的时间在那里创建ajax请求,但它们的行为完全相同):http://jsfiddle.net/Y26zd/

相关问题