我有两个单独的 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) { ... })
进一步阅读:
一篇关于如何在 jQuery 中使用 Promise 的好文章:http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/
与您需要的类似的小抽象示例(抱歉,没有足够的时间在那里创建 ajax 请求,但它们的行为完全相同):http://jsfiddle.net/Y26zd/
关于jquery - 将两个单独的ajax Post请求结果添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671348/