jquery - 将两个单独的ajax Post请求结果添加在一起

标签 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
       }
     });

任何帮助都将非常感激..谢谢:)

最佳答案

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

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

进一步阅读:

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

关于jquery - 将两个单独的ajax Post请求结果添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671348/

相关文章:

javascript - 按键检查不起作用

javascript - 如何使用 Prototype/JS 选择特定的动态生成元素?

javascript - 从网站(特别是 Trello.com)获取 JSON 文件

javascript - 使用 javascript 测试 url 可用性

javascript - "Resource interpreted as script but transferred with MIME type text/html."

jquery - 定时 jquery/返回顶部 html css 链接

javascript - jQuery - 当单击另一个链接时触发单击 div 内的另一个链接

jquery - 许多带有大字符串的模态窗口

javascript - 区分 AJAX 上的多种表单

javascript - 如何根据商品数量计算每件商品的价格?